【问题标题】:When I do Geospatial querying in MongoDB, how do I also return the distance from my desired point?当我在 MongoDB 中进行地理空间查询时,如何返回与所需点的距离?
【发布时间】:2010-10-29 22:28:41
【问题描述】:
for post in db.datasets.find({"loc":{"$near":[50,50]}}).limit(10):

如何获取文档与“50,50”之间的距离?

【问题讨论】:

    标签: python mongodb geospatial database


    【解决方案1】:

    使用“geoNear”命令将返回一个“dis”值作为结果的一部分。将此值乘以您选择的单位的地球半径,即可得出该结果与原始位置之间的距离。

    例如,其中“places”是集合的名称,原始点的 lat/lng 是 50、50。

    db.command( { geoNear : "places" , near : [50,50] } );
    

    将返回格式为:

    [
        {
            "dis" : 0.3886630122897946,
            "obj" : {
                "_id" : ObjectId("4d9123026ccc7e2cf22925c4"),
                "pos" : {
                    "lon" : -10,
                    "lat" : -20
                }
            }
        }
    ]
    

    将每个结果的“dis”值乘以 6371 将为您提供以公里为单位的距离,而 3959 则为英里。

    Mongo docs 中有更多详细信息和示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多