How to close cursor in MongoKit

2019-06-18 23:00发布

问题:

I'm using MongoKit to perform iteration over a huge amount of data.

During this process my cursor becomes invalid, and I'm getting

OperationFailure: cursor id '369397057360964334' not valid at server

I've read in mailing lists that I can pass parameter timeout=False to .find() method, but PyMongo FAQ says that I vave to take care of closing cursor myself.

But I didn't find methods in MongoKit for that.

Do I need to close cursor by hand, and if yes - how can I do it?

回答1:

You'll have to close the cursor since the MongoDB server won't time out the cursor for you, given that you specifically asked it not to.

simply call del on your cursor. The default pymongo implementation for __del__ will notify the server to kill the cursor.

Assuming something like:

cursor = db.test.find(timeout=False)

Simply do this when you're done:

del cursor