gpt4 book ai didi

mongodb - Geonear 不工作 - mongodb?

转载 作者:可可西里 更新时间:2023-11-01 10:00:37 26 4
gpt4 key购买 nike

我在集合 offers 中有以下示例 mongodb 文档。

集合:优惠

 {
"countryCode" : "LU",
"geoLocation" : [
{
"lat" : 49.8914114000000026,
"lng" : 6.0950994999999999
}
]
},
{
"countryCode" : "DE",
"geoLocation" : [
{
"lat" : 50.8218536999999984,
"lng" : 9.0202151000000015
}
]
}

要求:

根据半径50公里左右的地理位置获取文档

我尝试过的:

db.getCollection('offers').aggregate([
{
$geoNear: {
near: [6.0950994999999999, 49.8914114000000026],
distanceField: "geoLocation",
maxDistance: 50000, //50 km radius
}
}
]);

问题:

它正在获取所有文档,而不是搜索大约 50 公里。

任何建议将不胜感激。

最佳答案

旧版坐标对不返回以米为单位的距离,而是以弧度为单位。因此,您的 maxDistance 远大于 50km。参见 Calculate Distances with Spherical Geometry

但也要考虑这里使用的实际距离:

db.getCollection('offers').aggregate([
{
$geoNear: {
near: [6.0950994999999999, 49.8914114000000026],
distanceField: "distance"
}
]);

返回:

{
"_id" : ObjectId("55dc0f4b6f07ad5d3ec3b6d0"),
"countryCode" : "DE",
"geoLocation" : [
{
"lat" : 50.8218537,
"lng" : 9.020215100000001
}
],
"distance" : 60.588259822017925
}
{
"_id" : ObjectId("55dc0f4b6f07ad5d3ec3b6cf"),
"countryCode" : "LU",
"geoLocation" : [
{
"lat" : 49.8914114,
"lng" : 6.0950995
}
],
"distance" : 61.93733827090219
}

因为“distanceField”是报告距查询点的投影距离的字段。

因此,起点是您的文档对于地理空间查询无效。像这样修复它们:

首先删除所有索引:

db.offers.dropIndexes();

然后修复字段:

var bulk = db.offers.initializeOrderedBulkOp(),
count = 0;

db.offers.find({}).forEach(function(doc) {
//printjson(doc);

var tmp = {
"type": "Point",
"coordinates": [doc.geoLocation[0].lng,doc.geoLocation[0].lat]
};
doc.geoLocation = tmp;
printjson(doc);

bulk.find({ "_id": doc._id }).updateOne({
"$set": { "geoLocation": doc.geoLocation }
});
count++;

if ( count % 1000 == 0 ) {
bulk.execute();
bulk = db.offers.initializeOrderedBulkOp();
}
});

现在数据以正确的 GeoJSON 格式表示:

{
"_id" : ObjectId("55dc14856f07ad5d3ec3b6d1"),
"countryCode" : "LU",
"geoLocation" : {
"type" : "Point",
"coordinates" : [
6.0950995,
49.8914114
]
}
}
{
"_id" : ObjectId("55dc14856f07ad5d3ec3b6d2"),
"countryCode" : "DE",
"geoLocation" : {
"type" : "Point",
"coordinates" : [
9.020215100000001,
50.8218537
]
}
}

现在创建一个 2dsphere 索引:

db.offers.createIndex({ "geoLocation": "2dsphere" })

现在您可以查询了:

db.offers.aggregate([
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [6.0950994999999999, 49.8914114000000026]
},
"distanceField": "distance",
"spherical": true,
"maxDistance": 50000
}}
])

它返回一个“近”的文档,或者实际上是 0 距离:

{
"_id" : ObjectId("55dc14856f07ad5d3ec3b6d1"),
"countryCode" : "LU",
"geoLocation" : {
"type" : "Point",
"coordinates" : [
6.0950995,
49.8914114
]
},
"distance" : 0
}

关于mongodb - Geonear 不工作 - mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196916/

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