3

Hi I am fetching documents from mongdb using mongo_collection.find() , this collection has 600k documents. after traversing 400k documents I am geting below error

Traceback (most recent call last):
  File "mongo_opp.py", line 324, in <module>
    obj.indexAnswer()
  File "mongo_opp.py", line 241, in indexAnswer
    for dict_result in result:
  File "build/bdist.linux-x86_64/egg/pymongo/cursor.py", line 1032, in next
  File "build/bdist.linux-x86_64/egg/pymongo/cursor.py", line 974, in _refresh
  File "build/bdist.linux-x86_64/egg/pymongo/cursor.py", line 864, in __send_message
  File "build/bdist.linux-x86_64/egg/pymongo/helpers.py", line 112, in _unpack_response
pymongo.errors.CursorNotFound: Cursor not found, cursor id: 83302133311

The solutions to this problem is setting timeout paramter mongo_collection.find(timeout=False) which I found in below links,

StackOveflowLink1

StacKOverflowLink2

Setting timeout parameter makes cursor open for unlimited time, which will affect system performance. Can anybody help me with cursor close connection. Help appreciated :)

Community
  • 1
  • 1
3ppps
  • 933
  • 1
  • 11
  • 24

3 Answers3

1

Hey you can solve this using del keyword

result = mongo_collection.find(no_cursor_timeout=True)

del result

Check Link1, Link2

Community
  • 1
  • 1
1

To closing the cursor you need to remove the variable where you're put the query result

del cursor

or

cursor.close()

you can also fix the probleme by choosing a low value of batch_size:

(with Pymongo for example)

col.find({}).batch_size(10)
HISI
  • 4,557
  • 4
  • 35
  • 51
0

Python uses reference counting for object lifetime management, when the Cursor object goes out of scope the garbage collector would call __die() which closes the cursor. If you want explicit control, you can call close() by yourself.

Close() call __die() internally.Check this out

3ppps
  • 933
  • 1
  • 11
  • 24