Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Location With kilometer Radius in Mongodb?

Tags:

mongodb

I have a collection called restaurants.. and also i have formatted into geoJSON and I tried to query the following way

db.restaurants.find({ location:
   { $geoWithin:
      { $centerSphere: [ [ -73.93414657, 40.82302903 ], 5 / 3963.2 ] } } })

I have mentioned radius 5/3963.2 (calculation in miles).. but I need radius distance in kilometer like { $centerSphere: [ [ -73.93414657, 40.82302903 ], 5] }

I need to mentioned radius in kilometer

How to do it?

like image 537
Balakumar B Avatar asked Dec 23 '22 22:12

Balakumar B


1 Answers

MongoDB expects values in radians.

To convert distance to radians, simply divide the distance value to the radius of the sphere (earth), where:

3963.2 is radius of earth in Miles.

6378.1 is radius of earth in Km.

db.restaurants.find({ location:
   { $geoWithin:
      { $centerSphere: [ [ -73.93414657, 40.82302903 ], 5 / 6378.1 ] } } })
like image 152
sergiuz Avatar answered Jan 19 '23 18:01

sergiuz