Harsha Vardhan
Harsha Vardhan

Reputation: 101

Geospatial Queries in mongo db is not working with $maxdistance

I am using geospatial indexing in mongodb to find and list the objects in db that are near a particular location. Here I use $near and $maxDistance to find the objects within a particular kilometer range. I am confused here in giving the value for $maxDistance. Can anyone help me understand what value(either km, m, or rad) do I want to give for $maxDistance? Here is my db structure:

{
        "_id" : ObjectId("556719a7e0550ca9d16ca303"),
        "Name" : "Apl",
        "loc" : [
                11.485489,
                77.247103
        ]
}
{
        "_id" : ObjectId("556719b1e0550ca9d16ca304"),
        "Name" : "Ind",
        "loc" : [
                11.462442,
                77.252167
        ]
}
{
        "_id" : ObjectId("556719bfe0550ca9d16ca305"),
        "Name" : " Ara ",
        "loc" : [
                11.452179,
                77.276629
        ]
}
{
        "_id" : ObjectId("556719eae0550ca9d16ca306"),
        "Name" : " Akodi ",
        "loc" : [
                11.455739,
                77.299278
        ]
}
{
        "_id" : ObjectId("556719f6e0550ca9d16ca307"),
        "Name" : " Kasi ",
        "loc" : [
                11.459693,
                77.328804
        ]
}

Here I used the following query to list the objects that are 5 kms from a particular location.

db.place.find({loc:{$near:[11.503993, 11.503993],$maxDistance:1000}});

The above query returns the following:

{ "_id" : ObjectId("556719a7e0550ca9d16ca303"), "Name" : "Apl", "loc" :
[ 11.485489, 77.247103 ] }
{ "_id" : ObjectId("556719b1e0550ca9d16ca304"), "Name" : "Ind", "loc" :
[ 11.462442, 77.252167 ] }
{ "_id" : ObjectId("556719bfe0550ca9d16ca305"), "Name" : " Ara ", "loc"
: [ 11.452179, 77.276629 ] }
{ "_id" : ObjectId("556719eae0550ca9d16ca306"), "Name" : " Akodi ", "loc
" : [ 11.455739, 77.299278 ] }
{ "_id" : ObjectId("556719f6e0550ca9d16ca307"), "Name" : " Kasi ", "loc"
 : [ 11.459693, 77.328804 ] }

But If I use the query with $maxDistance:66 it returns nothing. What value should I be using for $maxDistance to get the objects with in 2 kms?

Upvotes: 1

Views: 1022

Answers (1)

user4103496
user4103496

Reputation:

Looks like min and max distance are measured in meters:

{
  $near: {
     $geometry: {
        type: "Point" ,
        coordinates: [ <longitude> , <latitude> ]
     },
     $maxDistance: <distance in meters>,
     $minDistance: <distance in meters>
  }
}

radians are used with legacy coordinates.

source: http://docs.mongodb.org/manual/reference/operator/query/near/

Upvotes: 1

Related Questions