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

问题描述 投票:0回答:3

当我使用

$geoNear is only valid as the first stage in a pipeline.
作为服务器端进行聚合时,出现以下错误
nodejs

聚合对象:

[ { '$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 } } ]

知道为什么我会收到上述错误吗?

javascript node.js database mongodb geolocation
3个回答
0
投票

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


0
投票

确保您正在关注的字段已在您的 $project 中标明。就像我遇到这个错误一样:

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

你明白了。

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


0
投票

对于任何正在寻找此内容的人。 Geonear 应该是聚合管道中的第一个操作。之后,您可以运行匹配、查找和项目查询。但第一个应该是 GeoNear 查询

© www.soinside.com 2019 - 2024. All rights reserved.