首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MongoCursor<Document>与FindIterable<Document>的区别

MongoCursor<Document>与FindIterable<Document>的区别
EN

Stack Overflow用户
提问于 2018-06-15 12:37:08
回答 2查看 3.6K关注 0票数 3

我想知道MongoCursor和FindIterable的区别是什么。

MongoCursor:

代码语言:javascript
运行
复制
MongoCursor<Document> cursorPersonDoc = personDocCollection.find(whereClauseCondition).iterator();
        while (cursorPersonDoc.hasNext()) {
           Document doc = cursorPersonDoc.next();
           String s1 = doc.getString("s1");
         }

FindIterable:

代码语言:javascript
运行
复制
FindIterable<Document> cursorPersonDoc = personDocCollection.find(whereClauseCondition);
    for (doc: cursorPersonDoc){
      String s1 = doc.getString("s1");
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-15 13:15:16

如果您查看这两个类中的方法,您将得到一个想法。

FindIterable有一些方法,如filterlimitskip,这些方法将帮助您过滤掉结果。

此外,它还有一些方法,如maxAwaitTime(用于可裁剪游标)和maxTime

MongoCursor并没有所有这些。但是使用MongoCursor有一个优势。MongoCursor接口扩展了MongoCursor,而后者又扩展了AutoCloseable。

AutoCloseable (在Java 7中引入)使得使用试用式资源成语成为可能。就像这样

代码语言:javascript
运行
复制
try (final MongoCursor cursor = personDocCollection.find(whereClauseCondition).iterator()) {
   ........
 }
票数 3
EN

Stack Overflow用户

发布于 2018-06-15 12:57:04

findIterablemongodbIterable文档

FindIterable

很容易找到。

MongoIterable

MongoIterable是操作(如查询)的结果。

MongoCursor允许迭代任何类型的数据,因为FindIterable只遍历Find结果。

FindIterable将为您提供更有针对性的方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50875787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档