【问题标题】:Example using geospatial indexing with pymongo使用 pymongo 进行地理空间索引的示例
【发布时间】: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 使用示例(比文档中的更好)都会有帮助。 谢谢

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    您使用的是旧形式的查询,其中距离以弧度指定。如果您更改为new format,则maxDistance 以米为单位指定。

    另外,由于时间的迷雾迷失,GeoSpatial 数据按 Long/Lat 顺序排序,因此您的所有坐标都需要反转。我已经修复了您的代码以使用正确的坐标方向并输入新的查询格式。我还为集合添加了drop 命令(因为这是示例代码)。这让我很困惑,它可能会让你感到困惑。多次运行 的程序将一遍又一遍地插入相同的点。没有滴 每个查询将返回与您运行程序的次数一样多的结果。最后我添加了一个GEOSPHERE 索引,文档说你需要它,即使你的程序在没有它的情况下运行良好。我怀疑如果没有这个,随着位置数量的增加,您会看到性能呈几何级下降。

    import pymongo
    import pprint
    from pymongo.errors import BulkWriteError
    
    db = pymongo.MongoClient().geo_example
    db.places.drop()
    db.places.create_index([("location", pymongo.GEOSPHERE)])
    
    cities = [{"location": {'type': 'Point', 'coordinates': [2, 57]}, "name": "Aberdeen"},
              {"location": {'type': 'Point', 'coordinates': [13, 52]}, "name": "Berlin"},
              {"location": {'type': 'Point', 'coordinates': [26, 44]}, "name": "Bucharest"},
              {"location": {'type': 'Point', 'coordinates': [14, 40]}, "name": "Napoli"},
              {"location": {'type': 'Point', 'coordinates': [2, 48]}, "name": "Paris"},
              {"location": {'type': 'Point', 'coordinates': [-70, 35]}, "name": "Tokyo"},
              {"location": {'type': 'Point', 'coordinates': [8, 47]}, "name": "Zurich"}]
    
    try:
        result = db.places.insert_many(cities)
        db.places.create_index([("location", pymongo.GEOSPHERE)])
        for doc in db.places.find({"location":{
                                    "$nearSphere": {
                                         "$geometry": {
                                            "type": "Point",
                                            "coordinates": [2,57]
                                          },
                                         "$maxDistance": 1}}}):
            pprint.pprint(doc)
    except BulkWriteError as bwe:
        print(bwe.details)
    
    

    【讨论】:

    • 是的,我知道drop,我只复制了部分测试代码。通常我只是做 db.command('dropDatabase') 只是删除所有内容。总之,非常感谢!我得到了我的错误,有时将 mongodb 转换为 pymongo 有点令人困惑!
    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 2013-05-05
    • 1970-01-01
    • 2018-07-28
    • 2011-04-19
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多