2017-01-01 58 views
0

我使用Mongo DB来存储坐标,我想使用$ near query查找坐标,但没有结果,有人可以帮我吗? 这里是我的蒙戈DB

> db.course.getIndexes() 
[ 
    { 
      "v" : 1, 
      "key" : { 
        "_id" : 1 
      }, 
      "name" : "_id_", 
      "ns" : "dev.course" 
    }, 
    { 
      "v" : 1, 
      "key" : { 
        "coordinate" : "2dsphere" 
      }, 
      "name" : "coordinate_2dsphere", 
      "ns" : "dev.course", 
      "2dsphereIndexVersion" : 3 
    } 
] 

指数这里是我的数据

{ "_id" : 1, "schoolId" : 0, "longitude" : 0, "latitude" : 0, "coordinate" : [ 0, 0 ], "courseTitle" : "暂无", "imageIds" : "[]", "videoIds" : "[]", "startTime" : "", "teacherIds" : "[]", "gradeIds" : "[]", "subjectId" : 0, "expense" : 0, "followNum" : 0, "courseStatus" : "NOSTART", "authorizationStatus" : "NO", "rejectReason" : "", "createTime" : "2017-01-01 16:58:22" } 

我查询

db.course.find({"coordinate": {"near": [123, 20]}}) 

没有结果。有谁能够帮助我?

回答

0

$near是运营商使用,如果你写near它只会匹配near字段下的coordinate字段。

$near请求将是这样的:

db.course.find({ 
    "coordinate": { 
     "$near": { 
      $geometry: { 
       type: "Point", 
       coordinates: [123, 20] 
      } 
     } 
    } 
}) 

要使用2dsphere索引文件应该GeoJSON格式需要的其它附加type领域:

{ type: "<GeoJSON type>" , coordinates: <coordinates> } 

所以,你只需要更换"coordinate" : [ 0, 0 ]附:

"coordinate": { 
    type: "Point", 
    coordinates: [0, 0] 
} 
+0

它的作品,非常感谢你!如果我使用2dsphere索引文件,那么我必须使用GeoJSON格式,但如果我使用2d索引文件,那么我可以使用GesJSON和传统坐标对,是吗? – Minky

+0

你能告诉我这个查询有什么问题:> db.course.find({“coordinate”:{“$ near”:[123,20],“$ limit”:100}}) – Minky

+0

是2d用于遗留坐标,2dspere为GeoJSon格式。你可以用'db.course.find({“coordinate”:{“$ near”:[123,20]}})来限制结果的数量,限制(100)' –