36

I am trying to get my head around MongoDB and geospatial searches. Basically what I want is for users to be able to query documents (images) that are shot within a set distance from the users current location. So I the user is searching for all images that are shot within 1 km from where he is standing I try to use the below example but I have no idea what to set as the maxdistance value.

db.places.find({ loc : { $near : [50,50] , $maxDistance : 5 }})

So my question is what do I set as maxdistance if I am searching for documents within a 1 km radius?

I am totally stuck here.

alexmac
  • 19,087
  • 7
  • 58
  • 69
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

3 Answers3

70

In order to use mongodb $near queries with km bounds, you need to convert the radius value to km. By default mongodb $near accepts $maxDistance as radius.

Convert distance by 111.12 (one degree is approximately 111.12 kilometers) when using km, or leave distance as it is on using degree

to your question

what do I set as maxdistance if I am searching for documents within a 1 km radius?

you can use this

   db.places.find( { loc : { $near : [50,50] , $maxDistance : 1/111.12 } } )

I have answered how to use mongo geospatial features here in detail. You can check out

Community
  • 1
  • 1
RameshVel
  • 64,778
  • 30
  • 169
  • 213
  • Cool, thanks! What is the difference between $near and $nearSphere? – Jonathan Clark Oct 24 '11 at 10:33
  • 13
    $near assumes an idealized model of a flat earth, meaning that an arcdegree of latitude (y) and longitude (x) represent the same distance everywhere. So you have to convert the radius by 111 or 69 to get the results.But $nearSphere you need to convert the radius by (6371 km or 3959 miles) to get it work... you can read more about [here](http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-TheEarthisRoundbutMapsareFlat) – RameshVel Oct 24 '11 at 13:39
  • Is it possible to query a fraction? – Karl Feb 03 '13 at 13:38
  • 3
    Here the units are mentioned in the docs. Took a while to find this... http://docs.mongodb.org/manual/reference/operator/maxDistance/ Thanks for the conversion, @RameshVel! More accurate conversion here: http://www.chemical-ecology.net/java/lat-long.htm shows 111.11999965975954 km / degree (I'm working at the meter scale, so accuracy is important). – ericsoco Jul 26 '13 at 20:10
  • I should add that value varies for longitude degrees as you move away from the equator. Makes sense... – ericsoco Jul 26 '13 at 20:23
  • Here's a question where this approach doesn't seem to work: http://stackoverflow.com/q/19496883/1331671 – Ron Oct 21 '13 at 14:13
16

Since version 2.4 of MongoDB you can specify the radius directly in meters as a value of $maxDistance, like this:

db.<collection>.find({loc: {$near : 
                               {$geometry : 
                                   {
                                    type: 'Point', 
                                    coordinates: [<longitude> , <latitude>]       
                                   }
                               }, 
                               $maxDistance: 10*100 
                           }
                     }); 
tarmes
  • 15,366
  • 10
  • 53
  • 87
Ivan Hristov
  • 3,046
  • 2
  • 25
  • 23
  • 4
    It depends on the index. If the index is `2d` this won't work, because a 2d-index assumes «radian format». If the index on the other hand is `2dsphere` you are correct. :-) Documentation: https://docs.mongodb.com/manual/reference/operator/query/near/#query-on-legacy-coordinates – qualbeen Feb 01 '17 at 12:16
  • Here, Max distance is in meter or kilometer? – Mijanur Rahman Apr 20 '21 at 04:56
0

In recent versions I am using Spring and Mongo 4.0.5. Important thing is type of index and location data type. I am using 2dsphere,org.springframework.data.mongodb.core.geo.GeoJsonPoint Later on it generate corresponding mongo query with geometry with distance in meters.

{ "location" : { "$nearSphere" : { "$geometry" : { "type" : "Point", "coordinates" : [18.341772, 43.834183] }, "$maxDistance" : 331.0 }