2014-09-29 62 views
0

我使用$within$centerSphere僅獲取具有特定半徑的圓內的集合文檔。

,我使用的查詢是:

{"value.pos":{"$within":{"$centerSphere": 
[[40.6861735,14.777354800000012],0.0007839325190887568]}}} 

其中0.0007839325190887568 = 5 * 6378.1(地球半徑)。

我需要的是顯示點與文檔元素位置之間的距離。

我爲nodejs使用了mongodb驅動程序。

收到距離後,我需要以編程方式命令結果,只有用戶需要它。

我不能使用貓鼬,因爲我需要改變我現有的代碼太多,所以我想使用標準的mongodb驅動程序。

有沒有解決方案?

謝謝!

回答

2

從2.4開始已被棄用。要做你想做的事,確保你至少在MongoDB 2.4上運行,並且在彙總流水線階段使用geoNear。有必要使用聚合來獲得結果中的距離。

db.places.aggregate([ 
    { 
     $geoNear: { 
      near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] }, 
      distanceField: "distance", 
      maxDistance: < your distance in meters>, 
      spherical: true 
     } 
    } 
]) 

確保您在value.pos上有2dsphere索引。