这里是Pymongo的查询
import mong #just my library for initializing
collection_1 = mong.init(collect="col_1")
collection_2 = mong.init(collect="col_2")
for name in collection_2.find({"field1":{"$exists":0}}):
try:
to_query = name['something']
actual_id = collection_1.find_one({"something":to_query})['_id']
crap_id = name['_id']
collection_2.update({"_id":id},{"$set":{"new_name":actual_id}},upset=True)
except:
open('couldn_find_id.txt','a').write(name)
所有这些都是从一个集合中获取一个字段,找到该字段的id并更新另一个集合的id。它可以工作大约1000-5000次迭代,但是周期性地失败,然后我必须重新启动脚本。你知道吗
> Traceback (most recent call last):
File "my_query.py", line 6, in <module>
for name in collection_2.find({"field1":{"$exists":0}}):
File "/home/user/python_mods/pymongo/pymongo/cursor.py", line 814, in next
if len(self.__data) or self._refresh():
File "/home/user/python_mods/pymongo/pymongo/cursor.py", line 776, in _refresh
limit, self.__id))
File "/home/user/python_mods/pymongo/pymongo/cursor.py", line 720, in __send_message
self.__uuid_subtype)
File "/home/user/python_mods/pymongo/pymongo/helpers.py", line 98, in _unpack_response
cursor_id)
pymongo.errors.OperationFailure: cursor id '7578200897189065658' not valid at server
^C
bye
有人知道这个失败是什么吗?我怎么能把它变成一个例外,即使在这个失败的情况下继续我的脚本?你知道吗
谢谢
问题的原因在pymongo's FAQ中描述:
这是因为collection.find()的
timeout
参数:将
timeout=False
传递给find
应该可以解决问题:但是,请确保正确关闭光标。你知道吗
另请参见:
相关问题 更多 >
编程相关推荐