2016-12-09 127 views
1

我有一個名爲餐廳集合..而且我已經格式化成GeoJSON的,我試圖查詢以下方式Mongodb公里半徑的查詢位置?

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

我已經提到半徑5/3963.2(以英里計算)..但我需要半徑在公里的距離就像{ $centerSphere: [ [ -73.93414657, 40.82302903 ], 5] }

我需要提到半徑公里

怎麼辦呢?

回答

2

MongoDB預計以弧度表示的值。

要距離轉換爲弧度,只需將距離值劃分到球體的半徑(大地),其中:

3963.2是在數地球的半徑。

6378.1是以公里爲單位的地球半徑。

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

如果你不煤礦PLS解釋這個計算..什麼是價值[email protected] Zaharie –

+0

也是我還有一個疑問..我已經使用蒙戈指南針..如何在蒙戈指南針做相同的查詢@先生。 Sergiu Zaharie –

+0

爲此,您應該發佈另一個問題。 – sergiuz