我正在编写一个聚合查询,试图匹配某个区域内的用户。我遵循了 3.6 版本的文档(我的版本是 mongodb
)。
db.collection('users').aggregate([
{$match: {
location: {
$geoNear: {
near: {type: 'Point', coordinates: [lng, lat]},
maxDistance: globalConf.maxDistance * 1000
}
}
}})
如果我在
find
上下文中使用此匹配项,它可以工作。如何修改查询才能在聚合上下文中运行?我收到的错误是
$geoNear、$near和$nearSphere在此上下文中不允许
。
$match
并将其用于顶层。 - undefinedusers
集合中,我应该把location
字段放在哪里? - undefinedlocation
字段?地理空间索引将为您完成这项工作。 - undefined