location : "2d" }) Your values will need to be "within earth like bounds" ★ latitude -180 to 180 ★ longitude -90 to 90 You can specify other ranges incase you're not dealing with 'earth like bounds' db.places.ensureIndex( { location : "2d" } , { min : -500 , max : 500 } ) page 2
that you are indexing matters. longitude/latitude - otherwise you’re results are going to be wildly inaccurate. db.places.insert({ location : [30, 40] }) page 3
longitude/latitude db.places.find({"location" : {"$near" : [32, 42]}}) Documents are returned in the order of distance from the location By default will return 100 documents, unless you specify a limit db.places.find({"location" : {"$near" : [32, 42]}}).limit(35) page 4
as part of the resultset. Handy if you needed to show the distance from document to your location db.runCommand({ geoNear: 'locations', near: [32,42], maxDistance: 10 }) e maxDistance argument limits the distance from the central point. is example below quotes '10' degrees, realistically your numbers will be much smaller page 8
useful. To the best of my knowledge you will need to calculate the distance in meters yourself. Question is how accurate you need your measurements to be. page 11
(generally) Need to note that MongoDB treats your location queries as earth is a at two dimensional plane. It's a case of tradeoffs with accuracy and speed, as not only is the earth not at, it's also not perfectly spherical. If your calculations are near the poles, cover a lot of a area or need to be really accurate, you’ll need to do some math… http://en.wikipedia.org/wiki/Haversine_formula ere is no wrapping at the poles and international date line, although mongo will raise an error page 12
you can split your geospatial indexes into smaller buckets. So that queries only utilise the indexes that are closest to them. eg: your search only uses the index for eastern Australia and not the whole world is is only appropriate if the documents are reasonably close together db.places.ensureIndex({ location : "geoHaystack", type : 1 }, { bucketSize : 10 }) “bucketSize” = degrees latitude/longitude page 13