$ geoNear (aggregate pipeline) does not return valid documents

I am not getting the correct results from using $geoNear in the aggregate pipeline. The same query using a typical find () query (using $near ) really returns the correct results.

BUT , when deleting the equality condition (on schedule.key ), both requests return the correct data.

$geoNear using an aggregate pipeline:

 db.place.aggregate( [ { $geoNear: { spherical: true, near: { type: "Point", coordinates: [ 18.416145, -33.911973 ] }, distanceField: "dist" } }, { $match: { "schedule.key": { "$eq": "vo4lRN_Az0uwOkgBzOERyw" } } } ]) 

$near find the query:

 db.place.find( { "point" : { $near: { type: "Point", coordinates: [ 18.416145,-33.911973 ] } }, "schedule.key" : { $eq : "vo4lRN_Az0uwOkgBzOERyw" } }) 

The document in this collection looks something like this:

 { "_id" : UUID("da6ccbb1-3c7a-45d7-bc36-a5e6007cd919"), "schedule" : { "_id" : UUID("587de5b7-a744-4b28-baa8-e6efb5f7f921"), "key" : "vo4lRN_Az0uwOkgBzOERyw" }, "point" : { "type" : "Point", "coordinates" : [ 18.425102, -33.922153 ] }, "name" : "Cape Town" } 

I created the corresponding index in the dot field:

 db.place.ensureIndex( { "point" : "2dsphere" } ); 
+5
source share
1 answer

This is not the same request at all. There is a separate difference in using a separate stage of $match , since the "filtering" is performed only after the "nearest results" found. This means that you potentially return "less" results, because the criteria are not issued in combination.

This is why there is a "query" option in $geoNear :

 db.place.aggregate( [ { $geoNear: { spherical: true, near: { type: "Point", coordinates: [ 18.416145, -33.911973 ] }, distanceField: "dist", query: { "schedule.key": { "$eq": "vo4lRN_Az0uwOkgBzOERyw" } } } } ]) 

Now the same request. Or it would be exactly the same if you used $nearSphere . Since $near does not take into account the curvature of the earth when calculating the distance. $nearSphere and $geoNear does.

But the main thing is to combine it with the "query" option, as this is the only way that you really get both criteria considered during the initial search.

+3
source

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


All Articles