【发布时间】:2010-10-29 22:28:41
【问题描述】:
for post in db.datasets.find({"loc":{"$near":[50,50]}}).limit(10):
如何获取文档与“50,50”之间的距离?
【问题讨论】:
标签: python mongodb geospatial database
for post in db.datasets.find({"loc":{"$near":[50,50]}}).limit(10):
如何获取文档与“50,50”之间的距离?
【问题讨论】:
标签: python mongodb geospatial database
使用“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 中有更多详细信息和示例。
【讨论】: