Mongodb - Search for GeoNear inside nested JSON objects

I need to get the closest place near a specific 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 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 that I get the message β€œCan canicalize query: BadValue geoNear must be top level expr” (error code: 17287). This is strange since I get the correct results when I query without $ near, but with $ elemMatch just to get the exact object with a specific value.

Thanks.

SOLVE!

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

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

PEACE!

+6
source share

Source: https://habr.com/ru/post/978844/


All Articles