MongoDB geoNear command result distance in kilometer

11,760

Solution 1

The distances are being returned in meters. To convert to kilometers, divide by 1000. You can have MongoDB do this using the distanceMultiplier option:

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

Solution 2

As I continued my research about this issue I found this there is two types of storing type for points

legacy point as this format

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

and GeoJSON point as this format

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

if we are using the legacy point the result would be in radian so we would convert it to kilometers using this statement

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

if we are using GeoJSON point the result would be in meters so we would convert it to kilometers using this statement as this answer to this question

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

solution in case of GeoJSON point format

for more details check this link, a bug report to MongoDB that explains it all.

Share:
11,760
Shweelan Leon Sam
Author by

Shweelan Leon Sam

Updated on June 20, 2022

Comments

  • Shweelan Leon Sam
    Shweelan Leon Sam almost 2 years

    I am using mongoDB to store GPS locations data as this GeoJSON document

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

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

    Update: I also ensured 2dspher index like follow

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

    I got the coordinates from Google maps Now if I search using this command I cant convert the distances in result to kilometers

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

    the result is as follow

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

    the first result is expected to be Zero since its the same point that I want to get the distance from source = destination coordinates, but still has value.

    the second value I cant convert to Kilometers, in Google distance calculator is about 5.26KM.

  • Shweelan Leon Sam
    Shweelan Leon Sam over 9 years
    Yes I already added 2dsphere index using db.test.ensureIndex( { loc : "2dsphere" } )
  • Shweelan Leon Sam
    Shweelan Leon Sam over 9 years
    This seems the most near answer to the actual distance, but in the geoNear documentation its mentioned that the distance is in radiant and if I want to convert I should multiply by the earth radius of the Earth. what do you think?
  • wdberkeley
    wdberkeley over 9 years
    It's not in radians here. Distance in radians between two points on the Earth can never exceed 2 * pi. Radians are returned when using legacy coordinate pairs.