简体   繁体   English

迭代巨大的集合时出现Mongo DB Cursor问题

[英]Mongo DB Cursor issue while iterating over a huge collection

I am iterating over a Mongo DB collection. 我正在迭代Mongo DB集合。 This collection has millions of documents in it. 该集合中有数百万个文档。 I am using java API for this iteration. 我正在使用java API进行此迭代。 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. 所以我们计划在这次迭代中使用Thread.sleep。 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? 此外,是否有可能以块的形式读取数据,例如一次读取100个文档?

Instead of using a cursor over the entire collection you can try paging through the collection by the _id. 您可以尝试通过_id对集合进行分页,而不是在整个集合上使用游标。 So each time query for 100 documents (order by _id) and keep the last _id you encounter. 因此,每次查询100个文档(按_id排序)并保留您遇到的最后一个_id。 Then on each consecutive query use a condition to fetch documents where _id > last _id from previous fetch. 然后在每个连续查询中使用条件来获取文档,其中_id> last _id来自先前的提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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