首页 > 解决方案 > 分页时MongoDB geoNear返回空白

问题描述

我试图带回一个配置文件列表。这很好用 find()

但我需要切换到聚合进行距离排序。

现在不是让我的全部 100 条记录每条记录 20 条。我得到了 40,就好像它没有找到其余的一样。

有什么帮助吗?(是的,我尝试过使用简单的 geoNear 过滤器。结果相同)

 await Profile.aggregate([
      {
        $geoNear: {
          near: {
            type: "Point",
            coordinates: [long, lat]
          },
          distanceMultiplier:
            req.user.distanceMetric === "km" ? 0.001 : 0.000621371, // meter to ki:miles
          distanceField: "distance",
          spherical: true,
          maxDistance: distance !== 100 ? distance * 1609.34 : 99999999999
        }
      },
      {
        $match: {
          $and: [
            {
              _id: {
                $ne: Profile.castID(req.user.profileID),
                $nin: doNotDisplayList.map(id => Profile.castID(id))
              }
            }
          ]
        }
      },
      { $skip: skip },
      { $limit: limit }
    ]).sort({
      online: -1
    });

标签: mongodbmongooseecmascript-6mongodb-query

解决方案


推荐阅读