MongoDB-pymongo.errors.CursorNotFound: Cursor
阿新 • • 發佈:2018-05-16
mongodb pymongo python, python3.
先從數據庫中取得所有數據 db[‘test‘].find({},{_id:0}),然後對結果進行for循環
對應於 "<", "<=", ">", ">=", "!="。
原子操作符:"$and“, "$or“, "$nor“。
先從數據庫中取得所有數據 db[‘test‘].find({},{_id:0}),然後對結果進行for循環
demos = db[‘demo‘].find({},{"_id": 0})
for cursor in demos:
? ? ? ? ?do_something()
但是當do_something函數耗時過長,在cursor上長時間沒有進行操作,引發cursor在mongodb服務端超時
解決方案
1、設置no_cursor_timeout = True,永不超時,遊標連接不會主動關閉,需要手動關閉
demos = db[‘demo‘].find({},{"_id": 0},no_cursor_timeout = True) for cursor in demos: ? ? ? ? do_something() demo.close() # 關閉遊標
2、設置batch_size返回文檔數,默認應該是20個文檔(記不清了233333),可以設置小一些
#每次只返回一個文檔
demos = db[‘demo‘].find({},{"_id": 0}).batch_size(1)
for cursor in demos:
? ? ? ? do_something()
註意:這種方法仍然會出現可能超過10分鐘任然沒有返回,比如你在do_something裏進行一些十分耗時的操作,具體采用哪種方法按實際情況而定.
補充知識點:
mongodb條件操作符,"$lt", "$lte", "$gt", "$gte", "$ne"就是全部的比較操作符,
原子操作符:"$and“, "$or“, "$nor“。
【2】:
db.runCommand(
{
distinct:"sofang_xinfang",key:"city"
}
) ---distinct 找出 city 字段一共多少種 ?
MongoDB-pymongo.errors.CursorNotFound: Cursor