Mongodb 在 collection.aggregate() 中拼接不支持 $geoNear。替代 $geoWithin 没有 includeLocs

Mongodb Stitch unsupport $geoNear in collection.aggregate(). Alternative $geoWithin does not have includeLocs

in this question suggested to use $geoNear and its option "includeLocs". However, this aggregate is unsupported in MongoDb Stitch function. You can read in documentation https://docs.mongodb.com/stitch/mongodb/actions/collection.aggregate/#unsupported-aggregation-stages
在 MongoDb Stitch 中,我可以使用 $geoWithin 轻松查询地理空间。但是我无法分离我的 return 模型。有没有其他选择 "includeLocs" 或者我如何过滤 $geoWithin 结果?

我的模型是:

[
  {
    "locations": [
      {
        "address": "bla bla bla",
        "city": "Avila Beach",
        "coordinates": [
          -120.73605,
          35.17998
        ],
        "country": "Usa",
        "prods": [
          {
            "brand": "kumcho",
            "cagtegory": "tire",
            "id": "1_kumcho"
          },
          {
            "brand": "micheline",
            "cagtegory": "tire",
            "id": "1_micheline"
          },
          {
            "brand": "setrr",
            "cagtegory": "rim",
            "id": "1_setrr"
          }
        ],
        "state": "5 Cities"
      },
      {
        "address": "data data data",
        "city": "Arvin",
        "coordinates": [
            -118.84151,
            35.21617
        ],
        "country": "Usa",
        "prods": [
        ],
        "state": "Bakersfield",
      }
    ],
    "name": "My Car",
    "admin": "John"
  }
]

根据这个模型,当我像下面这样查询时。

.find({"locations.coordinates": {$geoWithin: {$centerSphere: [[-120.55361687164304, 35.22037830812648], 15/3963.2]}}},{"locations.coordinates":1})
.toArray() 

综合选择。

const pipeline = [
    {$match: {"locations.coordinates": {$geoWithin: {$centerSphere: [[-120.55361687164304, 35.22037830812648], 15/3963.2]}}}}
  ];

const cursor = coll.aggregate(pipeline);

结果是:

[
  {
    "locations": [
      {
        "coordinates": [
          {
            "$numberDouble": "-120.73605"
          },
          {
            "$numberDouble": "35.17998"
          }
        ]
      },
      {
        "coordinates": [
          {
            "$numberDouble": "-118.84151"
          },
          {
            "$numberDouble": "35.21617"
          }
        ]
      }
    ]
  }
]

但我想要具有根目录的文档的特定数组对象,它与搜索到的坐标相匹配。

例如,我需要这样的东西。

搜索值:
lon = -120.55361687164304
lat = 35.22037830812648
kmml = 3963.2(这是英里数)
distance = 15

根据上面的搜索值,匹配到的模型坐标为:
"coordinates": [-120.73605, 35.17998]

所以我想要这样的结果:

[
  {
    "locations": [
      {
        "address": "bla bla bla",
        "city": "Avila Beach",
        "coordinates": [
          -120.73605,
          35.17998
        ],
        "country": "Usa",
        "prods": [
          {
            "brand": "kumcho",
            "cagtegory": "tire",
            "id": "1_kumcho"
          },
          {
            "brand": "micheline",
            "cagtegory": "tire",
            "id": "1_micheline"
          },
          {
            "brand": "setrr",
            "cagtegory": "rim",
            "id": "1_setrr"
          }
        ],
        "state": "5 Cities"
      }
    ],
    "name": "My Car",
    "admin": "John"
  }
]

是否可以在 mongodb 文档中 return 特定字段?

谢谢。

geoNear 自版本 4.0 起已弃用。你可以尝试使用 $near $geometry; https://docs.mongodb.com/manual/reference/operator/query/near/

 //Mile-Km to meters 
  if (kmml == 3963.2){
    distance = distance * 1609.344;
  }  else{
    distance = distance * 1000;
  }

    return coll
        .find({"location":{
              $near: {
                $geometry: {
                    type : "Point",
                    coordinates : [ lon, lat ]
              },
              $maxDistance: distance
            }
        }
        }).toArray();