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