$geoNear (aggregate pipeline) not returning correct documents

前端 未结 1 2031
夕颜
夕颜 2021-01-18 20:33

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

相关标签:
1条回答
  • 2021-01-18 21:03

    It's not the "same" query at all. There is a distinct difference in using a separate $match stage, since the "filtering" is only done "after" the "nearest resuts" are found. This means that you potentially return "less" results since the criteria is not issued in combination.

    That's 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 that's the same query. Or it would be exactly the same if you used $nearSphere. Since $near does not account for the curvature of the earth in distance calcuations. $nearSphere and $geoNear does.

    But the main point is combining with the "query" option, since that's the only way you truly get both criteria considered in the initial search.

    0 讨论(0)
提交回复
热议问题