Mongodb - Finding GeoNear within nested JSON objects

醉酒当歌 提交于 2021-01-27 04:11:14

问题


I need to get the closest place near a certain point using this data structure:

    [
    {
        "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
            }
        ],
    }
]

I used the following query in order to get what I need:

{
    "actual_data": {
        "$elemMatch": {
            "id": "260",
            "value.lngalt": {
                "$near": {
                    "$geometry": {
                        "type": "Point",
                        "coordinates": [
                            -73.9667,
                            40.78
                        ]
                    },
                    "$minDistance": 1000,
                    "$maxDistance": 5000
                }
            }
        }
    }
}

But after queering it, I get "Can't canonicalize query: BadValue geoNear must be top-level expr" (error code: 17287). It's strange since I do get the right results when I query without the $near but with $elemMatch only in order to get the exact object with particular value.

Thanks.

SOLVED!

for future refarance I used $geoWithin instead $near function. So my query looks like :

{
    "actual_data": {
        "$elemMatch": {
            "id": "260",
            "value.lnglat": {
                "$geoWithin": {
                    "$centerSphere": [
                        [
                            -71.07651880000003,
                            42.353068
                        ],
                        0.001
                     ]
                }
            }
        }
    }
}

PEACE!

来源:https://stackoverflow.com/questions/27193280/mongodb-finding-geonear-within-nested-json-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!