Search code examples
pythonpython-3.xpymongopymongo-3.x

How to solve attribute error: 'Cursor' object has no attribute 'noCursorTimeout' in pymongo?


I'm trying to set noCursorTimeout in the session following this instruction from MongoDB docs. But I'm getting the error: AttributeError: 'Cursor' object has no attribute 'noCursorTimeout'

I can't wonder why I'm getting this.

from pymongo import MongoClient as Connection
from datetime import datetime

conn = Connection(get_uri())
with conn as conn:
  collection = conn['db_name']['my_collection']
  documents_cursor = collection.find(query).noCursorTimeout() # same with .maxTimeMS()

Solution

  • According to the document, you should use it like this:

    cursor = collection.find({"a": 1}, no_cursor_timeout=True)
    

    Reference: https://api.mongodb.com/python/current/migrate-to-pymongo3.html#timeout-replaced-by-no-cursor-timeout