MongoDB geoWithin 缺失结果

MongoDB geoWithin missing results

查询一个非常简单的数据集,每次只有returns 3/4的结果。

我永远无法符拉迪沃斯托克展示。

查询

db.locations.find({"location": {"$geoWithin": {"$center" : [[43.1198, 131.8869], 4000]} } })

数据

{ 
    "_id" : ObjectId("5d826a4d1efa91047ca21941"), 
    "location" : {
        "type" : "Point", 
        "coordinates" : [
            59.9138, 
            10.7522
        ]
    }, 
    "name" : "Oslo"
},
{ 
    "_id" : ObjectId("5d826b1b258557047c43d397"), 
    "location" : {
        "type" : "Point", 
        "coordinates" : [
            59.9938, 
            10.7522
        ]
    }, 
    "name" : "North of Oslo"
},
{ 
    "_id" : ObjectId("5d826b33258557047c43d398"), 
    "location" : {
        "type" : "Point", 
        "coordinates" : [
            43.1198, 
            131.8869
        ]
    }, 
    "name" : "Vladivostok"
},
{ 
    "_id" : ObjectId("5d826b65258557047c43d399"), 
    "location" : {
        "type" : "Point", 
        "coordinates" : [
            -22.9068, 
            -43.1729
        ]
    }, 
    "name" : "Rio"
}

Mongodb 希望您存储为 [lng, lat]。

经度角度范围最大为 +180 度(东 180 度),最小为 -180 度(西 180 度)。 纬度角的范围最高可达 +90 度(或北纬 90 度),最低可达 -90 度(或南纬 90 度)。

    { 
    "_id" : ObjectId("5d826b33258557047c43d398"), 
    "location" : {
        "type" : "Point", 
        "coordinates" : [
            43.1198, 
            131.8869
        ]
    }, 
    "name" : "Vladivostok"
}

所以请检查坐标。 {131.8869, 43.1198}

If you use longitude and latitude, specify coordinates in order of longitude, latitude.

来自 $geoWithin

您的值始终按正常的 [latitude, longitude] 顺序排列,但对于 MongoDB、it is in reverse,这确实令人困惑 tbh

如果您在您的示例中恢复所有地理位置的顺序,您将获得结果中的所有四个点。

mongoplayground

上测试