2014-09-29 59 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索引。