MongoDB Cursor Timeout workaround

Source: Internet
Author: User
Tags mongodb mongodb client mongodb server
resolves a MongoDB cursor ID is not a valid at server issue
 

When you use Db.collection.find (), it returns not all data, but actually a "cursor". Its default behavior is to query the database for the first 101 documents, or 1 MB of documents, depending on which condition is met first, and then query 4 MB of documents each time the document in the cursor is exhausted. In addition, the default behavior of Find () is to return a cursor that expires after 10 minutes of no action. If I have a batch document not finished in 10 minutes, and then processed, and then with the same cursor ID to the server to remove a batch, the cursor ID of course has expired, which can explain why I got the cursor ID invalid error.
A workaround on Stack Overflow is to pass in Timeout=false at Find () to disable the 10-minute timeout protection. But I think this is a very bad way, because if you have an exception in the loop, or even a power outage or disconnection, will cause the MongoDB server resources can never be released. And the better way is (I also sent on the Stack Overflow), estimated a batch size, so that the MongoDB client each crawl of the document within 10 minutes to run out, so that the client has to contact the server at least 10 minutes to ensure that the cursor does not time out.

Specific usage:
For document in Db.collection.find (). Batch_size (30):

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.