gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:57:17 25 4
gpt4 key购买 nike

我正在使用 Mongoose 和带有 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]
}
},
....
});

询问:
model.find({
"location.coordinates": {
"$near": {
"$maxDistance": 1000,
"$geometry": {
"type": "Point",
"coordinates": [
10,
10
]
}
}
}
}).sort('-score');

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

最佳答案

在查找查询中,您需要使用 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);
});

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

db.collection.createIndex( { "location" : "2dsphere" } )

在mongodb中 $near文档它说:

$near sorts documents by distance. If you also include a sort() for the query, sort() re-orders the matching documents, effectively overriding the sort operation already performed by $near. When using sort() with geospatial queries, consider using $geoWithin operator, which does not sort documents, instead of $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);
});

为了计算半径,我们将公里除以 6378.1,将英里除以 3963.2,如 here 所述.

因此,这将找到 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"
}
]

输出:(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"
}
]

关于mongodb - Mongoose Geo Near Search - 如何在给定距离内排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61343311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com