简体   繁体   中英

Mongo DB Cursor issue while iterating over a huge collection

I am iterating over a Mongo DB collection. This collection has millions of documents in it. I am using java API for this iteration. This iteration exercise can go on for a while and we dont want to impact performance of the application. So we are planning to use Thread.sleep during this iteration. But we are facing cursor timeout kind of problems. Anyone faced this before? Also, is it possible to read data in chunks eg 100 documents at a time?

Instead of using a cursor over the entire collection you can try paging through the collection by the _id. So each time query for 100 documents (order by _id) and keep the last _id you encounter. Then on each consecutive query use a condition to fetch documents where _id > last _id from previous fetch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM