1

所以我的代码是:

FindIterable<Document> findIterable = collection.find().skip(20).limit(10);
MongoCursor<Document> cursor = findIterable.iterator();
while (cursor.hasNext()) {
    Document doc = cursor.next();
    // My stuff with documents here
}
cursor.close(); // in finally block

现在我想知道total count之前的文件skiplimit

完全相同的问题有一个答案,但我有,而且mongo-java-driver v3.1.0我使用的 API 不同。FindIterableMongoCursor类中没有方法count()或没有方法。谢谢!size()

更新

似乎唯一的解决方法是再次调用 mongodb:collection.count正如@Philipp 所说

4

1 回答 1

8

现在可以在MongoCollection中找到count方法:

int count = collection.count();

返回集合中的文档总数。

int count = collection.count(criteria);

返回集合中与给定条件匹配的文档数。

于 2015-11-23T15:13:30.783 回答