【pymongo】mongodb cursor id not valid error

参考来源:

http://stackoverflow.com/questions/10298354/mongodb-cursor-id-not-valid-error

http://stackoverflow.com/questions/24199729/pymongo-errors-cursornotfound-cursor-id-not-valid-at-server

 

mongodb cursor id not valid error是一个超时错误

当使用for  c  in  col.find()时,数据库会一次性返回很多数据,如果处理这些数据的时间超过10分钟,一直没有像数据库获取后续数据,则会出现上述错误。

 

解决方案:

取消timeout限制,在结束遍历后close()游标。

cursor = coll.find(timeout=False)
for c in cursor:
    ...
    ...
cursor.close()

 

 

其他方案:

上面那个方案是我使用后成功的。还有几种方案,我尝试了一下,无效,不知道为什么。

解决方案2:

用batch_size()限制一次获取的数据量

for c in col.find().batch_size(20):
    ...

 

解决方案3:

用no_cursor_timeout参数去掉时间限制。注意后面要close()游标。

cursor=db.images.find(no_cursor_timeout=True)
for i in cursor:
   .....
   .....
cursor.close()

 

posted @ 2016-11-10 20:23  匡子语  阅读(1811)  评论(0编辑  收藏  举报