LoginSignup
1
1

More than 3 years have passed since last update.

pymongo.errors.CursorNotFoundの対処法

Posted at

PyMongoでこのエラーが出た時の対処法が1つにまとめられていなかったので、ここに記述します。

こちらの記事によると、2つの原因が考えられるという。
https://qiita.com/koshilife/items/bece28d331cda5469c33

  • タイムアウトの設定時間超過
  • バッチ取得サイズ16MBを超えた時

タイムアウトの設定時間超過

タイムアウトの設定時間超過が原因の場合、no_cursor_timeout=Trueのオプションを追加する事によって解決ができる。そして、cursorは使用後に自分で閉じる必要があるので、cursor.close()を記述する。


cursor = collection.find(query, no_cursor_timeout=True)
cursor.close()

バッチ取得サイズの超過

バッチ取得サイズの超過が原因の場合、batch_size=10のオプションを追加する事によって解決ができる。ただしbatch_sizeは自分の環境に合わせて適切な値に設定してください。

cursor = collection.find(query, batch_size=10)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1