【问题标题】:Mongoose within().circle() and $near gives result distinct to Mongo QueryMongoose inside().circle() 和 $near 给出了与 Mongo Query 不同的结果
【发布时间】:2017-02-10 05:55:13
【问题描述】:

我有一个 Node.js 测试,其中多个对象模拟从地图中的某个点移开,并且我不断测试它们是否在该点的半径范围内。 每个对象对应一个具有嵌套旧版二维索引 'location.loc' 的模型。

我用过这个查询

var center={_lat:20,lng:-100};

var area = { 
      center: [center._lng,center._lat], 
      radius: 100,//This are supossed to be meters
      unique: true,
      spherical: true }


  MyModel.where('location.loc')
         .within()
         .circle(area)
         .exec(function (err, records) {
            if (err) { 
              throw err; 
            }
            //Log Length of records
         });

和

  MyModel.find({  
    "location.loc": {
      $near: [center._lng,center._lat],
      $maxDistance: 1 //This is suposed to be a mile
    }
    }).exec(function(err, documents) {
        if (err) { 
          throw err; 
        }
        //Log documents length
    });

但是这两个脚本都返回给我整个文档集合,即使它们距离该点数英里。 同时,我在我的 Mongo 客户端中使用 $near,$geoWithin 进行相同的查询,这些查询给了我正确的答案。

我的 Mongoose 脚本有什么问题?

对于这种情况,是否应该只使用within().circle()?

【问题讨论】:

    标签: node.js mongodb mongoose geospatial


    【解决方案1】:

    原来第一个代码中的半径定义错误。 在这个Mongo documentation 说

    使用球面:true,如果指定 GeoJSON 点,MongoDB 使用米作为测量单位

    但这是针对 GeoJSON 点和geoNear。
    对于传统坐标格式 [long,lat],我们必须将半径值(以英里为单位)除以 3963.2 以将其转换为弧度。看到这个Example。

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2020-07-03
      相关资源
      最近更新 更多