【问题标题】:pymongo db.collection.update operationFailurepymongo db.collection.update 操作失败
【发布时间】:2013-10-24 00:24:46
【问题描述】:

我有大量文档,我正在尝试使用pymongo.update 函数对其进行更新。我正在查找属于某个多边形的所有文档并更新所有 使用“update_value”找到的点。

for element in geomShapeCollection:
    db.collectionName.update({"coordinates":{"$geoWithin":{"$geometry":element["geometry_part"]}}}, {"$set":{"Update_key": update_value}}, multi = True, timeout=False)

对于较小的集合,此命令按预期工作。在最大的数据集中 该命令适用于 70-80% 的数据,然后抛出错误:

pymongo.errors.OperationFailure: 游标 id '428737620678732339' 不是 在服务器上有效

pymongo 文档告诉我这可能是由于超时问题。

如果 MongoDB 中的游标已打开,则它们可能会在服务器上超时 很长一段时间没有对其进行任何操作。

阅读 pymongo 文档,find() function 有一个用于超时的布尔标志。

find(spec=None, fields=None, skip=0, limit=0, timeout=True, snapshot=False, tailable=False, _sock=None, _must_use_master=False,_is_command=False)

但是update function 似乎没有这个:

update(spec, document, upsert=False, manipulate=False, safe=False, multi=False)

有没有办法为更新函数设置这个超时标志?有什么办法可以改变这个,这样我就不会得到这个 OperationFailure 错误?我是否正确假设这是一个超时错误,因为 pymongo states that it throws this error when

在数据库操作失败时引发。

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    经过一些研究和大量实验后,我发现导致错误的是外循环光标。

    for element in geomShapeCollection:
    

    geomShapeCollection 是 mongodb 集合的游标。 geoShapeCollection 中有几个元素落下大量元素,因为这些更新需要相当长的时间 geomShapeCollection 光标关闭。

    问题根本不在于更新功能。向外部光标添加 (timeout=False) 即可解决此问题。

    for element in db.geomShapeCollectionName.find(timeout=False):
        db.collectionName.update({"coordinates":{"$geoWithin":{"$geometry":element["geometry_part"]}}}, {"$set":{"Update_key": update_value}}, multi = True, timeout=False)
    

    【讨论】:

      猜你喜欢
      • 2016-03-18
      • 2015-07-29
      • 2021-10-01
      • 2011-07-24
      • 2021-07-20
      • 2016-11-12
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多