如何使用 $geoNear return 距离导致两个不同的子值

How to return distance along result in two different sub values using $geoNear

我有这个查询:

db.places.aggregate([
   { "$geoNear" : 
      { "near" : 
         { 
            "type" : "Point", "coordinates" : [23, 11] 
         }, "distanceField" : "distance",
          "spherical" : true 
      } 
   },
   { 
      "$sort" : 
      { "distance" : 1 } 
   },
   { 
      "$limit" : 10 
   }
])

哪个会 return

{
    "_id":ObjectId("XXXX"),
    "longitude":23.11,
    "latitude":11.1995,
    "distance":23.111995
}

但是,在 C# 等语言中,由于 "distance" 不是 returned 文档的 C# class.

的一部分,反序列化中断了。

我怎样才能得到像下面这样的结果?

{
    "document": {
        "_id":ObjectId("XXXX"),
        "longitude":23.11,
        "latitude":11.1995
    },
    "distance":23.111995
}

感谢您的帮助

您可以 运行 $project 重塑聚合结果。 $$ROOT 表示作为输入传递到管道阶段的文档:

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 } }
])