pymongo公司数据库.collection.update操作失败

2024-03-28 18:46:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很大的文档集合,我正试图使用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: cursor id '428737620678732339' not valid at server

pymongo文档告诉我,这可能是由于超时问题造成的。在

Cursors in MongoDB can timeout on the server if they’ve been open for a long time without any operations being performed on them.

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

^{pr2}$

但是update function似乎没有:

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

有没有办法为更新函数设置这个超时标志?有什么方法可以更改它,这样我就不会得到这个OperationFailure错误了?假设这是pymongostates that it throws this error when的超时错误,对吗

Raised when a database operation fails.


Tags: 数据函数in文档命令falseforvalue
1条回答
网友
1楼 · 发布于 2024-03-28 18:46:26

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

for element in geomShapeCollection:

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

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

^{pr2}$

相关问题 更多 >