Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB geoNear command result distance in kilometer

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.

like image 500
Shweelan Leon Sam Avatar asked Dec 02 '22 18:12

Shweelan Leon Sam


2 Answers

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.

like image 80
Shweelan Leon Sam Avatar answered Jan 17 '23 16:01

Shweelan Leon Sam


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] } })
like image 34
wdberkeley Avatar answered Jan 17 '23 16:01

wdberkeley