我需要使用此数据结构获得最接近某一点的位置:
[
{
"data_id": "127",
"actual_data": [
{
"id": "220",
"value": "shaul"
},
{
"id": "221",
"value": "3234324"
},
{
"id": "222",
"value": {
"lngalt": [
13.7572225,
-124.0429047
],
"street_number": null,
"political": null,
"country": null,
}
},
{
"id": "223",
"value": "dqqqf1222fs3d7ddd77@Dcc11cS2112s.com"
},
{
"id": "224",
"value": "123123"
},
{
"id": "225",
"value": "lala1"
},
....
},
{
"data_id": "133",
"actual_data": [
{
"id": "260",
"value": {
"lngalt": [
1.7572225,
32.0429047
],
"street_number": null,
"political": null,
"country": null,
}
},
{
"id": "261",
"value": -122.25
}
],
}
]
我使用以下查询以获得我需要的内容:
{
"actual_data": {
"$elemMatch": {
"id": "260",
"value.lngalt": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [
-73.9667,
40.78
]
},
"$minDistance": 1000,
"$maxDistance": 5000
}
}
}
}
}
但是在排队之后,我得到“无法规范查询:BadValue geoNear必须是顶级expr”(错误代码:17287)。这很奇怪,因为当我在没有$ near但只使用$ elemMatch进行查询时,我确实得到了正确的结果,以便获得具有特定值的确切对象。
感谢。
解决!
为了将来的折射我用$ geoWithin而不是$ near函数。所以我的查询如下:
{
"actual_data": {
"$elemMatch": {
"id": "260",
"value.lnglat": {
"$geoWithin": {
"$centerSphere": [
[
-71.07651880000003,
42.353068
],
0.001
]
}
}
}
}
}
乎!