是 FindIterable<Document>加载所有文件? [英] is FindIterable&lt;Document&gt; load all documents?

查看:46
本文介绍了是 FindIterable<Document>加载所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我使用 MongoCollection 获取所有文档,返回类型为 FindIterable,然后遍历 Iterable 来处理每个文档.

Currently, I'm using MongoCollection<Document> to get all documents, the return type is FindIterable<Document>, then loop over Iterable to process each document.

例如:

FindIterable<Document> docs = getCollection().find();
for(Document doc : docs) {
    ...
}

但我不知道 FindIterable 是否会加载所有文档并循环它,或者它只是加载游标并稍后在循环时获取文档?

But I don't know whether FindIterable will load all documents and loop it or it's just loads the cursor and fetch the documents later when looping?

推荐答案

您要的是 Mongo Java Driver 的实现细节,因此不是公共接口的一部分,因此不是保证在以后的版本中保持不变.

What you are asking for is an implementation detail of the Mongo Java Driver, and thus not part of the public interface and thus not guaranteed to stay the same in future versions.

除此免责声明之外,从 3.2.x 开始,find() 调用在幕后创建了一个 FindOperationIterable,它使用 MongoBatchCursorAdapter 作为其迭代器,而迭代器又基于 BatchCursor.

This disclaimer aside, as of 3.2.x, the find() call creates a FindOperationIterable under the hood, which uses a MongoBatchCursorAdapter as its iterator, which in turn is based on a BatchCursor.

批处理游标中的javadoc说明:

The javadoc in the batch cursor states:

"MongoDB批量返回查询结果,此接口提供了这些批次的迭代器.第一次调用下一个方法将返回第一批,后续调用将触发获取下一批结果的请求.客户端可以控制通过设置调用 next 之间的 batchSize 属性来批量大小."

"MongoDB returns query results as batches, and this interface provideds an iterator over those batches. The first call to the next method will return the first batch, and subsequent calls will trigger a request to get the next batch of results. Clients can control the batch size by setting the batchSize property between calls to next."

因此,它会分批读取结果,如果当前批次已经完全读取,则再次查询数据库.

Thus, it will read results in batches, and query the database again, if the current batch has been read completely.

这篇关于是 FindIterable<Document>加载所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆