Balakumar B
Balakumar B

Reputation: 790

Query Location With kilometer Radius in 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?

Upvotes: 6

Views: 4404

Answers (1)

sergiuz
sergiuz

Reputation: 5529

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 ] } } })

Upvotes: 13

Related Questions