MongoDB geoNear命令结果距离以千米为单位

Shw*_*Sam 4 gps distance geospatial mongodb geojson

我使用mongoDB存储GPS位置数据作为此GeoJSON文档

{"type":"Point","coordinates":[33.313183,44.465632]}

{"type":"Point","coordinates":[33.2926487,44.4159651]}

更新:我也确保2dspher索引如下

db.test.ensureIndex( { loc : "2dsphere" } )
Run Code Online (Sandbox Code Playgroud)

我从谷歌地图获得坐标现在,如果我使用此命令搜索,我无法将结果中的距离转换为公里

db.runCommand({geoNear: 'test', spherical: true, near: {type: "Point" , coordinates: [33.2926487, 44.4159651]}})
Run Code Online (Sandbox Code Playgroud)

结果如下

"results" : [
    {
        "dis" : 0.0033427770982422957,
        "obj" : {
            "_id" : ObjectId("54a96f348fa05fcaed637a22"),
            "loc" : {
                "type" : "Point",
                "coordinates" : [
                    33.2926487,
                    44.4159651
                ]
            }
        }
    },
    {
        "dis" : 5764.7060911604085,
        "obj" : {
            "_id" : ObjectId("54a96f4c8fa05fcaed637a23"),
            "loc" : {
                "type" : "Point",
                "coordinates" : [
                    33.313183,
                    44.465632
                ]
            }
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

第一个结果预计为零,因为我想要从源=目标坐标获得距离,但仍然有价值.

我无法转换为公里的第二个值,在Google距离计算器中约为5.26KM.

Shw*_*Sam 8

当我继续研究这个问题时,我发现这有两种类型的存储类型

遗留点作为这种格式

db.test2.insert({location: [33.313183, 44.465632]})
Run Code Online (Sandbox Code Playgroud)

和GeoJSON指向这种格式

db.test.insert({location: {"type" : "Point", "coordinates" : [33.313183, 44.465632]}})
Run Code Online (Sandbox Code Playgroud)

如果我们使用遗留点,结果将是弧度,所以我们将使用此语句将其转换为千米

db.runCommand({geoNear: 'test2', spherical: true, near: [33.2926487, 44.4159651], distanceMultiplier: 6371})
Run Code Online (Sandbox Code Playgroud)

如果我们使用GeoJSON点,结果将以米为单位,因此我们将使用此语句将其转换为公里作为此问题的答案

db.runCommand({geoNear: 'test', spherical: true, near: {type: "Point" , coordinates: [33.2926487, 44.4159651]}, distanceMultiplier: 0.001})
Run Code Online (Sandbox Code Playgroud)

GeoJSON点格式的解决方案

有关更多详细信息,请查看此链接,这是一个针对MongoDB的错误报告


wdb*_*ley 7

距离以米为单位返回.要转换为千米,除以1000.您可以让MongoDB使用以下distanceMultiplier选项执行此操作:

db.runCommand({ "geoNear" : "test", "spherical" : true, "distanceMultiplier" : 0.001, "near" : { "type" : "Point" , "coordinates" : [33.2926487, 44.4159651] } })
Run Code Online (Sandbox Code Playgroud)