$geoNear仅在管道中的第一阶段有效



在使用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 } } ]

知道我为什么会出现上面的错误吗?

检查是否有任何执行聚合的预中间件函数。如果是这样,那么这很可能就是错误的来源。

确保您正在关注的字段已显示在$项目中。就像我在中遇到了这个错误

[
{
$project: {
title: 1,
date: 1,
}
{
$match: {
company, //company is a variable
},
},
]

你明白了。

然后,我遇到类似问题的另一个原因是$sort和$match不能出现在$search之前,所以请尝试重新排序您的管道。

最新更新