18

I have a collection of stores with a geospacial index on the location propery.

What I am trying to do is given the user's latitude, latitude and a search radius (mi), I want to return the list of stores that are within those parameters.

I saw the following example on the MongoDB documentation (http://www.mongodb.org/display/DOCS/Geospatial+Indexing), but it looks like the distance is in radians.

center = [50, 50]
radius = 10
db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})

So, what is the formula to convert miles to radians?

Solution The awesome people at mongodb-user helped me find the answer. Basically, what I was really looking for was a way to limit the distance.

Per, the updated documentation, there is a 3rd parameter to the $near query: You can also use $near with a maximum distance

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

The value for $maxDistance is in radians; so, I had to take my distance in miles and divide it by 69.

Thank you!

Abe
  • 6,386
  • 12
  • 46
  • 75
  • I was able to find my answer by posting my question on the mongdb-user Google Group (http://groups.google.com/group/mongodb-user/browse_thread/thread/d37c781bbfff5f08). – Abe Oct 12 '10 at 00:06
  • 5
    Note: dividing by 69 is a rough conversion from miles to *degrees*, not radians. Degrees are correct here because this is a plain ol' `$near` query (which means `$maxDistance` must be in the same units as the coordinates are), not a spherical query (which *do* require `$maxDistance` to be in radians). – John Flatness May 17 '11 at 03:15

2 Answers2

17

As docs say:

All distances use radians. This allows you to easily multiply by the radius of the earth (about 6371 km or 3959 miles) to get the distance in your choice of units. Conversely, divide by the radius of the earth when doing queries.

so you simple need to divide your radius by the earth radius. in your example it would be:

radius = 10/3959
walla
  • 1,087
  • 8
  • 9
  • Sorry, I don't think that's it. I am getting no results and when I run the equivalent in SQL Server I get results – Abe Oct 07 '10 at 19:24
  • What version of MongoDb do you using? – walla Oct 08 '10 at 05:09
  • I am using version 1.6.3 for Windows 64 bit – Abe Oct 10 '10 at 08:16
  • 13
    If anyone else is confused by this answer and still getting incorrect results, I would try multiplying or dividing by the earth's radius * pi/180, not just by the radius. – Matt Diamond Mar 30 '11 at 21:55
  • 3
    Actually if anybody is still having issues with this, the calculation above is correct. 1 radian = 3959 miles (on earth), so to convert miles to radians you simply divide by 3959. The real problem is you should be using $centerSphere instead of $center. Ran into the same thing myself. – Adam Mar 19 '15 at 21:13
0

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

let if your radius is in km then use

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

but if your radius in miles then convert it in km

Manoj Rana
  • 3,068
  • 1
  • 24
  • 34