使用$ nearSphere

时间:2016-05-06 05:32:40

标签: mongodb mongodb-query

我的地方集合中有以下行。

{ "_id" : ObjectId("572b3892967c76c62b7f63e3"), "loc" : { "type" : "Point", "coordinates" : [ -73.97, 40.77 ] }, "name" : "Central Park", "category" : "Parks" }

{ "_id" : ObjectId("572b38ba967c76c62b7f63e4"), "loc" : { "type" : "Point", "coordinates" : [ -73.88, 40.78 ] }, "name" : "La Guardia Airport", "category" : "Airport" }

我无法执行以下查询

db.places.find({$nearSphere: {$geometry: { type: "Point", coordinates: [-73.92, 40.775]}, $minDistance: 10000, $maxDistance: 10}})

我收到以下错误: Error: error: { "$err" : "Can't canonicalize query: BadValue unknown top level operator: $nearSphere", "code" : 17287 }

请告诉我这是什么问题。

1 个答案:

答案 0 :(得分:0)

在查询对象中," loc"字段丢失。此外,我认为你宣布minDistance和maxDistance错误,因为maxDistanace< minDistance才会。

我认为您想要的查询类似于:

db.places.find({loc:{$nearSphere: {$geometry: { type: "Point", coordinates: [-73.92, 40.775]}, $minDistance: 10, $maxDistance: 10000}}})