MongoDB Geospatial Queries

MongoDB 22 พ.ค. 2020

Find within a Distance

https://docs.mongodb.com/manual/tutorial/geospatial-tutorial/

The following will find all restaurants within five miles of the user:

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

ถ้าต้องการกำหนดค่า Calculate Distance เป็น Miles

3963.2 is radius of earth in Miles.

ถ้าต้องการกำหนดค่า Calculate Distance เป็น Km

6378.1 is radius of earth in Km.

Ref: https://stackoverflow.com/questions/41059429/query-location-with-kilometer-radius-in-mongodb

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.