通过查找获得子文档的 2dsphere 索引的 MongoDB 聚合

Jun*_*ung 5 geolocation mongodb aggregation-framework

我在 MongoDB 中有两种类型的集合,它们的文档看起来像

店铺

{
    "_id" : ObjectId("589601db9635d288491dbe8f"),
    "name" : "shopA",
    ...,
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            126.928059, 
            37.522789
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

菜单

{
    "_id" : ObjectId("589601db9635d288491dbe8f"),
    "shopId": ObjectId("589601db9635d288491dbe8f"),  //MenuA belongs to shopA
    "name" : "menuA",
    ...,
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            126.928059, 
            37.522789
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

其中“location”是“2dsphere”索引。

我想获取一定距离内的商店“geoNear”的列表,以及一定距离内商店为“geoNear”的菜单列表。

第一个查询可以通过

db.shops.aggregate([
{
  $geoNear: {
    near: {
      type: "Point",
      coordinates: [lng, lat]
    },
    distanceField: "distance",
    maxDistance: 1000,
    spherical: true
  }
}])
Run Code Online (Sandbox Code Playgroud)

但怎么可能进行第二次查询呢?我必须将“geoNear”带到菜单的子文档商店,所以我尝试了

db.menus.aggregate([
{
  $lookup: {
    from: 'shops',
    localField: 'shopId',
    foreignField: '_id',
    as: 'shop'
  }
}, {
  $unwind: '$shop'
}, {
  $geoNear: {
     near: {
      type: "Point",
      coordinates: [lng, lat]
    },
    distanceField: "distance",
    maxDistance: 1000,
    spherical: true
  }
}])
Run Code Online (Sandbox Code Playgroud)

但它现在正在按我的预期工作。它说

“$geoNear 仅作为管道中的第一阶段有效。”

有没有什么办法可以实现我想要的,或者在MongoDB中本来是不可能的?

感谢您提前提供的帮助。