【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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM