MongoDB Java驱动程序3.4+;光标到数组

MongoDB Java驱动程序3.4+;光标到数组,java,mongodb,mongodb-query,mongodb-java,Java,Mongodb,Mongodb Query,Mongodb Java,这个问题非常简单明了。我不想使用游标,因为它大大增加了我的操作时间,我想一次获取所有文档并通过方法执行缓存它们 MongoClient mongoClient = new MongoClient( "HOST" ); MongoDatabase db = mongoClient.getDatabase( "DB" ); MongoCollection<Document> collection = db.getCollection( "C

这个问题非常简单明了。我不想使用游标,因为它大大增加了我的操作时间,我想一次获取所有文档并通过方法执行缓存它们

        MongoClient mongoClient = new MongoClient( "HOST" );
        MongoDatabase db = mongoClient.getDatabase( "DB" );
        MongoCollection<Document> collection = db.getCollection( "COLLECTION" );
        FindIterable<Document> iterable = externalobjects.find( new Document( "test", "123" ) );
MongoClient-MongoClient=新的MongoClient(“主机”);
MongoDatabase db=mongoClient.getDatabase(“db”);
MongoCollection collection=db.getCollection(“collection”);
FindIterable iterable=externalobjects.find(新文档(“test”,“123”);

所以,我想把上面的
iterable
转换成一个列表,如何使用查找并将游标转换为数组?

查找表不包含您“找到”的文档,而是充当服务器端游标的句柄,为了从服务器端游标中提取数据并将其存储在应用程序中的列表中,您必须从游标中读取文档并将其添加到应用程序中列表例如:

// Java 8
List<Document> asList = new ArrayList<>();
documents.forEach((Consumer<Document>) d -> asList.add(d));

// Guava
List<Document> asList = Lists.newArrayList(documents);

// old skool
List<Document> asList = new ArrayList<>();
for (Document d : documents) {
    asList.add(d);
}

操作对不起,减少的字数应该增加:D谢谢你的回答编辑了我的问题,谢谢,我也这么想,但我认为应该有一个简单的方法来实现它,就像老司机一样,它有一个
toArray
功能
int batchSize = ...;
FindIterable<Document> iterable = externalobjects
    .find(new Document("test", "123"))
    .batchSize(batchSize);