2013-10-24 22 views
1

我有一大堆文件,我试图使用pymongo.update函数进行更新。我正在查找属于某个多边形的所有文档,并更新了所有使用“update_value”找到的点。pymongo db.collection.update operationFailure

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有超时布尔标志。

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

Raised when a database operation fails.

回答

3

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

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)