MongoDB 的cursor.count()
和方法有什么区别?cursor.size()
DBCursor
问问题
11522 次
3 回答
32
从MongoDB Java Driver 的 Javadoc 中,它说:
DBCursor.count():计算与查询匹配的对象数。这没有考虑限制/跳过。
DBCursor.size():计算与查询匹配的对象数。这确实考虑了限制/跳过。
于 2012-08-09T10:29:44.133 回答
2
不仅仅是一个答案,我还想指出我们团队面临“混合”这两者的一个问题。
我们有这样的事情:
DBCursor cursor = collection.find(query).limit(batchSize);
logger.info("{} items found.", cursor.count());
while (cursor.hasNext()) {
...
}
事实证明,在调用该cursor.count()
方法后,限制被忽略了(请看一下另一个问题),我们想知道查询返回了多少项目,所以我们应该调用该cursor.size()
方法,因为调用那个count
确实产生不良的附带影响。
我希望这对其他人有帮助,因为找到我们面临的问题的根源并不容易。
于 2015-10-31T05:16:20.823 回答
0
cursor.count
当我第一次阅读有关and之间区别的文档时cursor.size
,我同样被难住了 b/c 我不明白不考虑skip
or是什么意思limit
。我发现这篇文章很有帮助,请在此处阅读更多内容。我认为以下示例说明了差异
// by default cursor.count ignores limit or skip. note that 100 records were returned despite the limit being 5
> db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).limit(5).count();
100
// if you want to consider limits and skips, then add an optional parameter specifying so
> db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).limit(5).count(true);
5
// cursor.size on the other hand abides by limits and skips
> db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).limit(5).size();
5
于 2021-08-13T23:20:25.467 回答