Node.js 如何在$centerSphere内使用$INTERNATED获得距离

Node.js 如何在$centerSphere内使用$INTERNATED获得距离,node.js,mongodb,geospatial,Node.js,Mongodb,Geospatial,我使用$within$centerSphere仅获取位于具有特定半径的圆内的集合文档 我使用的查询是: {"value.pos":{"$within":{"$centerSphere": [[40.6861735,14.777354800000012],0.0007839325190887568]}}} 其中0.0007839325190887568=5*6378.1(地球半径) 我需要的是显示点和文档元素位置之间的距离 我正在为nodejs使用mongodb驱动程序 在收到距离后,我只需要以

我使用
$within
$centerSphere
仅获取位于具有特定半径的圆内的集合文档

我使用的查询是:

{"value.pos":{"$within":{"$centerSphere":
[[40.6861735,14.777354800000012],0.0007839325190887568]}}}
其中
0.0007839325190887568
=
5*6378.1
(地球半径)

我需要的是显示点和文档元素位置之间的距离

我正在为nodejs使用mongodb驱动程序

在收到距离后,我只需要以编程方式对结果进行排序,而且只有用户需要

我不能使用mongoose,因为我需要更改太多现有代码,所以我想使用标准mongodb驱动程序

有解决办法吗


谢谢

自2.4版以来,$within已被弃用。要执行您想要的操作,请确保至少运行MongoDB 2.4并在聚合管道阶段使用。需要使用聚合来获得结果中的距离

db.places.aggregate([
    {
        $geoNear: {
            near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
            distanceField: "distance",
            maxDistance: < your distance in meters>,
            spherical: true
        }
    }
])
db.places.aggregate([
{
$geoNear:{
附近:{键入:“点”,坐标:[-73.99279,40.719296]},
距离字段:“距离”,
最大距离:<以米为单位的距离>,
是的
}
}
])
确保在
value.pos
上有一个2dsphere索引