【问题标题】:MongoDB geonear and distance conversionMongoDB 几何和距离转换
【发布时间】:2016-03-04 05:47:23
【问题描述】:

我有一个基于 2dsphere(球形)的 geonear 查询,它返回距离。我不完全确定返回的单位度量。是米还是弧度?我应该使用哪个“乘数”来获得以英里为单位的距离?我发现这个问题的答案都不同。

【问题讨论】:

    标签: mongodb-query


    【解决方案1】:

    结果通常以弧度返回

    查询:

    db.runCommand( {
       geoNear: "restaurants" ,
       near:  [ -73.93414657, 40.82302903 ],
       spherical: true,
    
    } )
    

    会给出这样的结果:

    {
    "dis" : 0.0001360968348384049,
    "obj" : {
            "_id" : ObjectId("55cba2476c522cafdb054fee"),
            "location" : {
                    "coordinates" : [
                            -73.94387689999999,
                            40.8255961
                    ],
                    "type" : "Point"
            },
            "name" : "Texas Star Snack Bar"
    }
    }
    

    所以在这种情况下,距离是半径,我们需要:

    1. multiply it by 3963.2 to get distance in miles (in example we have 0.539 M)
    
    2. multiply it by 6371 to get distance in kilometers (in example we have 0.867 km)
    

    如果您使用的是 3.2 并使用 minDistance 和 maxDistance 进行查询(根据定义以米为单位) - 前提是距离以米为单位

        db.runCommand(
       {
         geoNear: "restaurants",
         near: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
         spherical: true,
    
         minDistance: 3000,
         maxDistance: 7000
       }
    )
    

    和示例结果:

    {
    "dis" : 338.44550608396395,
    "obj" : {
            "_id" : ObjectId("55cba2476c522cafdb0561eb"),
            "location" : {
                    "coordinates" : [
                            -73.9653551,
                            40.7828647
                    ],
                    "type" : "Point"
            },
            "name" : "(Public Fare) 81St Street And Central Park West (Delacorte Theatre)"
    }}
    

    关于 here 的数据集有一个很好的教程 - 你不能用于测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2017-12-10
      • 2018-09-03
      • 2022-09-22
      • 2013-03-02
      相关资源
      最近更新 更多