Mongodb Mongo查询,其中$geointen和$near在一起
我的用例是,我希望获得一个城市内的所有记录,并且附近的某个位置应该是第一位的。 假设我有以下记录:Mongodb Mongo查询,其中$geointen和$near在一起,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我的用例是,我希望获得一个城市内的所有记录,并且附近的某个位置应该是第一位的。 假设我有以下记录: {"name" : "A", "location" : {"point" : [latlngs1]}}, //LatLng1 is in cityA {"name" : "B", "location" : {"point" : [latlngs2]}}, //LatLng2 is in cityB {"name" : "C", "location" : {"point" : [latlngs3]}
{"name" : "A", "location" : {"point" : [latlngs1]}}, //LatLng1 is in cityA
{"name" : "B", "location" : {"point" : [latlngs2]}}, //LatLng2 is in cityB
{"name" : "C", "location" : {"point" : [latlngs3]}}, //LatLng3 is in cityA
{"name" : "D", "location" : {"point" : [latlngs4]}} //LatLng4 is in cityA
我希望能在城内的latlng5附近找到所有记录
说距离bw位置点
[latlng5, latlng1] --> 10 unit
[latlng5, latlng3] --> 5 unit
[latlng5, latlng4] --> 8 unit
所以结果应该是这样的:
{"name" : "C"},
{"name" : "D"},
{"name" : "A"}
通过我的研究,我发现$geointen
最适合应用有界查询,因此我可以使用$geointen查找城市内的记录
db.TEST.aggregate([{$match:{“location.point”:{$geointen:{$box:
[[northEastLngLat],[SouthWestLngLat]]}},{$project:
{name:1}}]).pretty()
这带来了记录:A、C和D
首先,对于附近的地方,我发现$geoNear
适合这里。
因此,查询类似于:
b、 TEST.aggregate([{“$geoNear”:{near:[latlng5],距离字段:
“距离”,球面:真},{$match:{“location.point”:
{$GEOIN:{$box:[[northEastLngLat],[SouthWestLngLat]]},
{$project:{name:1}}}]).pretty()
但这不会产生任何输出。我尝试将latlngs更改为$near,有时在少数情况下,它会产生1个结果,而在其他情况下则会产生0个结果
我的问题出了什么问题,请帮忙。
谢谢您应该试试这个,它将在10公里内获取所有记录
db.city.find({
location :{
$near : {
$geometry : {
index : "Point" ,
coordinates : [19.1, 72.89]
},
$maxDistance : 10000
}
}
})
它将返回所有记录,并确保您在位置上有索引