Question

I'm trying to build a spacial query on my MongoDB database (in order to find all results using geoNear.

It works, except that the calculated distance is not accurate at all. So, here is what I have:

> db.status.find()
{
    "_id":NumberLong(5),
    "location":{
        "latitude":48.8947767,
        "longitudes":2.103352099999997
    }
}

I have an index:

> db.status.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "aroundb.status",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "location" : "2dsphere"
        },
        "ns" : "aroundb.status",
        "name" : "location_2dsphere"
    }
]

And here is my query:

db.runCommand({ geoNear:"status",near: [ 48.8947767, 2.093352099999997], spherical:true, distanceMultiplier:6371} )

The result is:

{
"ns" : "aroundb.status",
"results" : [
    {
        "dis" : 1.1119514223177513,
        "obj" : {
            "_id" : NumberLong(5),
            "location" : {
                "latitude" : 48.8947767,
                "longitudes" : 2.103352099999997
            }

        }
    }
],
"stats" : {
    "time" : 14,
    "nscanned" : 1,
    "avgDistance" : 1.1119514223177513,
    "maxDistance" : 1.1119514223177513
},
"ok" : 1

}

As you can see, the calculated distance (multiplied by the earth radius in order to have it calculated in km) is 1.1119514223177513 km.

If I calculate the distance on the website http://www.lexilogos.com/calcul_distances.htm the calculated distance is 0.7312508492264875 km.

Using Google Maps the distance is also 0.7312508492264875 km.

So, the question is: what I'm doing wrong?

Was it helpful?

Solution

MongoDB requires coordinates in [longitude, latitude] format. In your case it is [latitude, longitude] and it doesn't really matter if you label the fields to mark that order is different than expected:

"location":{
   "latitude":48.8947767,
   "longitudes":2.103352099999997
}

is still interpreted as [long: 48.8947767, lat: 2.103352099999997]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top