Thursday, 20 August 2020

MongoDB - distanceFiled not working in $geoNear aggregation stage

Hello, so below is my aggregation pipeline (regarding fetching the restaurants). Everything is working fine, however the fetched documents do not contain field "distanceFromUser" that I speciefied earlier as distanceField in $geoNear stage. Why is that happening, I'm stuck with it.​Here is the quick explenation of this whole query (the app purpose is to fetch restaurant based on cryterias):The first stage is the $geoNear stage.The second one is limiting restaurants by the cuisines they serve.The third, forth and fifth one are regarding adding the avgGrade field that is based on all reviews each restaurant has.Then I'm just doing the sorting, projecting and pagination operations.​[ { "$geoNear": { "near": { "type": "Point", "coordinates": [ -74.075908, 40.606466 ] }, "distanceField": "distanceFromUser", "spherical": true, "maxDistance": 10000, "distanceMultiplier": 6378.16 } }, { "$match": { "cuisine": { "$in": [ "Polish", "Vegetarian" ] } } }, { "$unwind": "$grades" }, { "$group": { "_id": "$_id", "numberOfGrades": { "$sum": 1 }, "sumOfAllGrades": { "$sum": "$grades.score" }, "name": { "$first": "$name" }, "borough": { "$first": "$borough" }, "cuisine": { "$first": "$cuisine" }, "streetAddress": { "$first": "$address.street" }, "streetNumber": { "$first": "$address.building" } } }, { "$addFields": { "avgGrade": { "$divide": [ "$sumOfAllGrades", "$numberOfGrades" ] } } }, { "$sort": { "distanceFromUser": -1 } }, { "$project": { "sumOfAllGrades": 0 } }, { "$skip": 0 }, { "$limit": 10 } ]

Submitted August 20, 2020 at 09:59AM by bartekw2213

No comments:

Post a Comment