Mongoose Geo Near Search - 如何在给定距离内排序?

Dan*_*and 4 mongoose mongodb geojson

我正在使用猫鼬和带有 maxDistance 的近查询来过滤靠近给定 gps 位置的元素。但是,near 查询会覆盖其他排序。我想要的是在给定点的 maxDistance 内找到所有元素,然后按其他一些属性排序。这是我目前正在做的一个例子:

架构:

mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    score: {
        type: Number,
        required: true,
        default: 0
    },
    location: {
        type: {
            type: String,
            default: 'Point',
        },
        coordinates: {
            type: [Number]
        }
    },
    ....
});
Run Code Online (Sandbox Code Playgroud)

询问:

model.find({
  "location.coordinates": {
    "$near": {
      "$maxDistance": 1000,
      "$geometry": {
        "type": "Point",
        "coordinates": [
          10,
          10
        ]
      }
    }
  }
}).sort('-score');
Run Code Online (Sandbox Code Playgroud)

在 find 之后添加 .sort 在这里没有帮助,并且无论如何都会以接近的顺序返回项目。

Sul*_*Sah 5

在查找查询中,您需要使用location而不是location.coordinates.

router.get("/test", async (req, res) => {
  const lat = 59.9165591;
  const lng = 10.7881978;
  const maxDistanceInMeters = 1000;

  const result = await model
    .find({
      location: {
        $near: {
          $geometry: {
            type: "Point",
            coordinates: [lng, lat],
          },
          $maxDistance: maxDistanceInMeters,
        },
      },
    })
    .sort("-score");

  res.send(result);
});
Run Code Online (Sandbox Code Playgroud)

要使 $near 工作,您需要相关集合上的 2dsphere 索引:

db.collection.createIndex( { "location" : "2dsphere" } )
Run Code Online (Sandbox Code Playgroud)

在 mongodb $near docs 中,它说:

$near 按距离对文档进行排序。如果您还为查询包含 sort(),sort() 会对匹配的文档重新排序,有效地覆盖 $near 已经执行的排序操作。对地理空间查询使用 sort() 时,请考虑使用 $geoWithin 运算符,而不是 $near,它不对文档进行排序。

由于您对按距离排序不感兴趣,正如 Nic 指出的那样,使用 $near 是不必要的,好像这样使用$geoWithin

router.get("/test", async (req, res) => {
  const lat = 59.9165591;
  const lng = 10.7881978;
  const distanceInKilometer = 1;
  const radius = distanceInKilometer / 6378.1;

  const result = await model
    .find({
      location: { $geoWithin: { $centerSphere: [[lng, lat], radius] } },
    })
    .sort("-score");

  res.send(result);
});
Run Code Online (Sandbox Code Playgroud)

要计算半径我们把公里处6378.1,并且描述英里3963.2这里

因此,这将找到半径 1 公里内的位置。

示例文档:

[
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7741692,
                59.9262198
            ]
        },
        "score": 50,
        "_id": "5ea9d4391e468428c8e8f505",
        "name": "Name1"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7736078,
                59.9246991
            ]
        },
        "score": 70,
        "_id": "5ea9d45c1e468428c8e8f506",
        "name": "Name2"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7635027,
                59.9297932
            ]
        },
        "score": 30,
        "_id": "5ea9d47b1e468428c8e8f507",
        "name": "Name3"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7635027,
                59.9297932
            ]
        },
        "score": 40,
        "_id": "5ea9d4971e468428c8e8f508",
        "name": "Name4"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7768093,
                59.9287668
            ]
        },
        "score": 90,
        "_id": "5ea9d4bd1e468428c8e8f509",
        "name": "Name5"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.795769,
                59.9190384
            ]
        },
        "score": 60,
        "_id": "5ea9d4e71e468428c8e8f50a",
        "name": "Name6"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.1715157,
                59.741873
            ]
        },
        "score": 110,
        "_id": "5ea9d7d216bdf8336094aa92",
        "name": "Name7"
    }
]
Run Code Online (Sandbox Code Playgroud)

输出:(1km以内,按分数降序排列)

[
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7768093,
                59.9287668
            ]
        },
        "score": 90,
        "_id": "5ea9d4bd1e468428c8e8f509",
        "name": "Name5"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7736078,
                59.9246991
            ]
        },
        "score": 70,
        "_id": "5ea9d45c1e468428c8e8f506",
        "name": "Name2"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.795769,
                59.9190384
            ]
        },
        "score": 60,
        "_id": "5ea9d4e71e468428c8e8f50a",
        "name": "Name6"
    },
    {
        "location": {
            "type": "Point",
            "coordinates": [
                10.7741692,
                59.9262198
            ]
        },
        "score": 50,
        "_id": "5ea9d4391e468428c8e8f505",
        "name": "Name1"
    }
]
Run Code Online (Sandbox Code Playgroud)