我的mongoDb中有以下记录:
{
"_id" : ObjectId("56e63313059484f212a4305f"),
"title" : "The video title",
"location" : {
"coordinates" : [
-73.9667,
40.78
],
"type" : "Point"
},
}我想在那个确切的位置找到所有的点。如果我写:
db.videos.find({
location: {
'$nearSphere': {
'$geometry': {
type: 'Point',
coordinates: [-73.9667, 40.78]
},
'$maxDistance': 0
}
}
})[0]我没有得到任何结果。如果我输入:
db.videos.find({
location: {
'$nearSphere': {
'$geometry': {
type: 'Point',
coordinates: [-73.9667, 40.78]
},
'$maxDistance': 0.00001
}
}
})[0]我得到了一个结果。
我看了又看,在文档中没有任何地方说maxDistance不能是0。
可以吗?
发布于 2016-03-24 02:26:18
因此,将$maxDistance设置为0将被忽略,就像您没有设置该选项一样。
如果你正在寻找一个“完全匹配”,那就去问吧。从任何描述来看,它都不是$nearSphere查询:
db.videos.find({
"location.coordinates" : [ -73.9667, 40.78 ]
})当然,它将返回与“确切”位置相匹配的文档。
对于一些更复杂的内容,则使用聚合$geoNear代替。它投射出一个“距离”,您可以在以后的筛选中使用该“距离”:
db.videos.aggregate([
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [ -73.9667, 40.78 ]
},
"distanceField": "distance",
"spherical": true
}},
{ "$match": { "distance": 0 } }
]) 因此,初始管道级返回一个“距离”,第二阶段过滤掉任何结果,其中的距离实际上是0。
如果查询的对象类似于“多边形”或其他与“精确”坐标数组不匹配的GeoJSON表单,那么您真的应该需要这样做。
一般的“精确”数组匹配应该适合于“点”数据。
https://stackoverflow.com/questions/36191062
复制相似问题