參考來源:
http://stackoverflow.com/questions/10298354/mongodb-cursor-id-not-valid-error
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()
