小编Jun*_*ung的帖子

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

我在 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”带到菜单的子文档商店,所以我尝试了 …

geolocation mongodb aggregation-framework

5
推荐指数
0
解决办法
529
查看次数