MongoDB geoNear result in kilometer

I use mongoDB to store GPS location data like this GeoJSON document

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

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

Update: I also provided a 2dspher index, e.g.

db.test.ensureIndex( { loc : "2dsphere" } )

I got the coordinates from Google maps. Now, if I use this command, I can’t convert the resulting distances to kilometers

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

The result is as follows:

"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
                ]
            }
        }
    }
]

it is expected that the first result will be zero, since its the same point that I want to get the distance from the source = destination coordinates, but it still matters.

the second value, which I can’t convert to kilometers, in Google’s distance calculator is about 5.26 km.

+4
3

. , 1000. MongoDB distanceMultiplier:

db.runCommand({ "geoNear" : "test", "spherical" : true, "distanceMultiplier" : 0.001, "near" : { "type" : "Point" , "coordinates" : [33.2926487, 44.4159651] } })
+6

,

db.test2.insert({location: [33.313183, 44.465632]})

GeoJSON

db.test.insert({location: {"type" : "Point", "coordinates" : [33.313183, 44.465632]}})

, , ,

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

GeoJSON, , , ,

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

GeoJSON

, MongoDB, .

+4

You have indicated this field as 2dsphere. This may explain the inconsistency as you requestsphere: true

0
source

Source: https://habr.com/ru/post/1569912/


All Articles