I have this query:
db.places.aggregate([
{ "$geoNear" :
{ "near" :
{
"type" : "Point", "coordinates" : [23, 11]
}, "distanceField" : "distance",
"spherical" : true
}
},
{
"$sort" :
{ "distance" : 1 }
},
{
"$limit" : 10
}
])
Which would return
{
"_id":ObjectId("XXXX"),
"longitude":23.11,
"latitude":11.1995,
"distance":23.111995
}
However, in languages such as C#, in breaks the deserialization as "distance" isn't part of returned document's C# class.
How would I be able to get the result like the following?
{
"document": {
"_id":ObjectId("XXXX"),
"longitude":23.11,
"latitude":11.1995
},
"distance":23.111995
}
Thanks for any help
You can run $project to reshape your aggregation result. $$ROOT
represents the document that's being passed to a pipeline stage as input:
db.places.aggregate([
{ "$geoNear" : { "near" : { "type" : "Point", "coordinates" : [23, 11] }, "distanceField" : "distance", "spherical" : true } },
{ "$sort" : { "distance" : 1 } },
{ "$limit" : 10 },
{ "$project:": { "document": "$$ROOT", "distance": 1 } },
{ "$project": { "document.distance": 0, "_id": 0 } }
])