【发布时间】:2026-02-12 07:40:01
【问题描述】:
我在使用 nodejs 作为服务器端进行聚合时收到以下错误 $geoNear is only valid as the first stage in a pipeline.。
聚合对象:
[ { '$geoNear':
{ near: [Object], distanceField: 'distance', spherical: true } },
{ '$lookup':
{ from: 'reviews',
localField: '_id',
foreignField: 'restaurant',
as: 'reviews' } },
{ '$project':
{ logo: '$$ROOT.logo',
name: '$$ROOT.name',
cuisine: '$$ROOT.cuisine',
rate: [Object],
reservation: '$$ROOT.reservation',
closeAt: '$$ROOT.closeAt',
openAt: '$$ROOT.openAt',
recommendationsLength: [Object],
isLive: '$$ROOT.isLive',
avgCost: '$$ROOT.avgCost',
creationDate: '$$ROOT.creationDate',
distance: '$$ROOT.distance' } },
{ '$sort': { distance: 1 } } ]
知道为什么会出现上述错误吗?
【问题讨论】:
-
它在
mongoshell 中工作吗? -
我没有在 monogo shell 中尝试过我只在 nodejs @Oleg 上工作
-
那就试试shell吧。
-
通过聚合管道的代码是什么?它是否在将其发送到 MongoDB 服务器之前添加了另一个阶段?
-
尝试在您的 mongodb 上启用数据库分析,这样您就可以看到发送的确切查询。
标签: javascript node.js database mongodb geolocation