如何在mongoose和nodejs中使用$ geoNear
我试图显示两个坐标之间的距离。 下面的查询执行并按预期给出结果
db.runCommand( { geoNear: "distances", near: [ 83.307974, 17.716456], spherical: true } );
我想从nodejs脚本执行此查询。 我正在使用mongoose。 我试过聚合函数,但我没有得到结果。 有人能以正确的方式引导我!
我试过这个:
db.getCollection('distances').aggregate([ { "$geoNear": { "near": { "type": "Point", "locc": [83.307974, 17.716456] }, "distanceField": "distance", "spherical": true } } ]);
参考文献:
geoNear Stackoverflow问题
geoNeam MongoDB
尝试将“locc”更改为“坐标”,并确保为包含坐标的集合创build了2d或2dsphere索引。
db.getCollection('distances').aggregate([ { "$geoNear": { "near": { "type": "Point", "coordinates": [83.307974, 17.716456] }, "distanceField": "distance", "spherical": true } } ]);