【发布时间】:2020-04-08 07:53:14
【问题描述】:
我一直在考虑使用 MongoDB 而不是自定义地理空间数据库,但是我很难理解 pymongo 如何使用球坐标。 具体来说,我不确定 $maxDistance (和类似的有任何效果)。 例如,如果我执行此代码:
db = pymongo.MongoClient().geo_example
db.places.create_index([("location", pymongo.GEOSPHERE)])
cities = [{"location": {'type': 'Point', 'coordinates': [57, 2]}, "name": "Aberdeen"},
{"location": {'type': 'Point', 'coordinates': [52, 13]}, "name": "Berlin"},
{"location": {'type': 'Point', 'coordinates': [44, 26]}, "name": "Bucharest"},
{"location": {'type': 'Point', 'coordinates': [40, 14]}, "name": "Napoli"},
{"location": {'type': 'Point', 'coordinates': [48, 2]}, "name": "Paris"},
{"location": {'type': 'Point', 'coordinates': [35, -70]}, "name": "Tokyo"},
{"location": {'type': 'Point', 'coordinates': [47, 8]}, "name": "Zurich"}]
try:
result = db.places.insert_many(cities)
for doc in db.places.find( {"location":{'$nearSphere': [57, 2], "$maxDistance": 1}}).limit(3):
pprint.pprint(doc)
except BulkWriteError as bwe:
print(bwe.details)
我希望答案只是阿伯丁,即使最大距离大于 1 米,我仍然会得到最接近的 3 个。我想我做错了什么。 任何帮助以及更好的 pymongo 使用示例(比文档中的更好)都会有帮助。 谢谢
【问题讨论】: