【问题标题】:Does geoNear aggregation not work when the query field is the objectId?当查询字段为objectId时,geoNear聚合是否不起作用?
【发布时间】:2021-06-20 12:59:48
【问题描述】:

我的 geonear 代码可以正常工作,除非我输入查询:{id: req.params.id}。它适用于我放置的任何其他过滤器对象。

有谁知道在使用 geoNear 时如何使用 objectId 查询某个文档?

我目前有以下内容。是否还有任何方法可以首先对文档进行匹配,即{$match:{id:req.params.id}。我试过这样做,但由于 $geoNear 必须是管道中的第一步,它没有成功。

const distances = await User.aggregate([
      {
        $geoNear: {
          near: {
            type: "Point",
            coordinates: [lng, lat],
          },
          //   maxDistance: 13.3333 * 1000,
          spherical: true,
          distanceField: "distance",
          distanceMultiplier: 0.001,
          query: { _id: req.params.id },
        },
      },
      { $project: { id: 1, distance: 1 } },
    ]);

【问题讨论】:

    标签: mongodb


    【解决方案1】:
    猜你喜欢
    • 2020-07-15
    • 2017-07-18
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多