Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Units to use for maxdistance and MongoDB?

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.

like image 396
Jonathan Clark Avatar asked Oct 20 '11 14:10

Jonathan Clark


People also ask

Does MongoDB support geospatial data?

In MongoDB, you can store geospatial data as GeoJSON objects or as legacy coordinate pairs.

What is distanceField in geoNear?

The distanceField option specifies the field that will contain the calculated distance. $geoNear requires a geospatial index. If you have more than one geospatial index on the collection, use the keys parameter to specify which field to use in the calculation.


2 Answers

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

like image 106
RameshVel Avatar answered Nov 03 '22 15:11

RameshVel


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                             }                      });  
like image 44
Ivan Hristov Avatar answered Nov 03 '22 13:11

Ivan Hristov