【问题标题】:How to use $geoNear in mongoose and nodejs如何在 mongoose 和 nodejs 中使用 $geoNear
【发布时间】:2015-12-04 09:56:02
【问题描述】:

我正在尝试显示两个坐标之间的距离。下面的查询按预期执行并给出结果

db.runCommand( { geoNear: "distances",
             near: [ 83.307974, 17.716456],
             spherical: true
           }  );

我想从 nodejs 脚本执行这个查询。我正在使用猫鼬。 我尝试了聚合函数,但未能获得结果。谁能指导我正确的道路!

我试过这个:

 db.getCollection('distances').aggregate([
  { 
        "$geoNear": {
            "near": {
                 "type": "Point",
                 "locc": [83.307974, 17.716456]
             },
             "distanceField": "distance",

             "spherical": true

         }
    }
]);

参考资料:

geoNear Stackoverflow question
geoNeam MongoDB

【问题讨论】:

    标签: node.js mongodb mongoose geospatial


    【解决方案1】:

    尝试将“locc”更改为“coordinates”,并确保您已为包含坐标的集合创建了 2d 或 2dsphere 索引。

    db.getCollection('distances').aggregate([
      { 
            "$geoNear": {
                "near": {
                     "type": "Point",
                     "coordinates": [83.307974, 17.716456]
                 },
                 "distanceField": "distance",
    
                 "spherical": true
    
             }
        }
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-09
      • 2020-03-29
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多