我在MongoDB中有两种类型的集合,它们的文档看起来像
{
"_id" : ObjectId("589601db9635d288491dbe8f"),
"name" : "shopA",
...,
"location" : {
"type" : "Point",
"coordinates" : [
126.928059,
37.522789
]
}
}
{
"_id" : ObjectId("589601db9635d288491dbe8f"),
"shopId": ObjectId("589601db9635d288491dbe8f"), //MenuA belongs to shopA
"name" : "menuA",
...,
"location" : {
"type" : "Point",
"coordinates" : [
126.928059,
37.522789
]
}
}
其中"位置"是一个" 2dsphere"索引。
我想获得一个商店列表" geoNear"在某个距离,还有一个菜单列表,其商店是" geoNear"在某个距离。
可以使用
进行第一次查询db.shops.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [lng, lat]
},
distanceField: "distance",
maxDistance: 1000,
spherical: true
}
}])
但是如何进行第二次查询呢?我必须采取一个" 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
}
}])
但它现在按照我的预期工作了。它说
" $ geoNear仅作为管道中的第一个阶段有效。"
有没有办法实现我想要的,或者MongoDB最初是不可能的?
感谢你提前得到你的帮助。