【问题标题】:mongodb maxDistance attribute not querying proper radiusmongodb maxDistance 属性未查询正确的半径
【发布时间】:2015-12-08 17:52:57
【问题描述】:

插入数据库

db.users.insert({"first_name":"alex","temp":"ossington-dundas","loc":{"lat":43.64911870668915,"lon":-79.42063808441162}}) db.users.insert({"first_name":"heather","temp":"orillia","loc":{"lat":44.610634506093,"lon":-79.44514274597168}}) db.users.insert({"first_name":"lindsay","temp":"404-davis dr.","loc":{"lat":44.06834703351985,"lon":-79.4205093383789}}) db.users.insert({"first_name":"vanessa","temp":"bathurst-lawrence","loc":{"lat":43.71925681186761,"lon":-79.42960739135742}}) db.users.insert({"first_name":"ariel","temp":"yonge-dundas","loc":{"lat":43.65477003252322,"lon":-79.388108253479}}) db.users.insert({"first_name":"john","temp":"yonge-dundas","loc":{"lat":43.65477003252322,"lon":-79.388108253479}})

索引数据库:

db.users.ensureIndex({loc:"2d"})

从数据库中查询:

db.users2.find({"loc":{ $near: [ 43.65477003252322, -79.388108253479 ]}}).pretty()

结果:成功!

问题:$maxdistance

现在,我想在这些查询中设置 10 英里(最好是公里)的最大距离。所以查询应该是这样的:

db.users.find({"loc":{ $near: [ 43.65477003252322, -79.388108253479 ],$maxDistance: 10/3959}});

但 maxdistance 无法正常工作。似乎是什么问题?还有什么我应该使用的更有效的东西吗?喜欢 $centerSphere 谢谢!

【问题讨论】:

    标签: mongodb mongodb-query geospatial


    【解决方案1】:

    您正在划分弧度,这是从旧坐标对返回的测量值不正确。除数应为 3963.2(英里)或 6378.1(公里)。如果您刚开始将数据作为 GeoJSON 插入,则测量值始终以米为标准返回。

    但你的主要问题是你的坐标“错误的方式”。顺序是“经度”然后是“纬度”,所以你需要:

    { "$near": [ -79.388108253479, 43.65477003252322 ], "$maxDistance": 10/3963.2 }
    

    再一次,如果您首先使用了 GeoJSON,那么您将不会再感到困惑,因为订单是“强制”的,这与您使用“lat”和“lon”键解决它的方式不同传统的配对时尚。

    更不用说在地球的“地球”上测量距离时,你应该使用$nearSphere,它可以正确补偿。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多