Difference between cursor.count() and cursor.size() in MongoDB

强颜欢笑 提交于 2019-12-30 05:46:27

问题


What is the difference between the cursor.count() and cursor.size() methods of MongoDB's DBCursor?


回答1:


From the Javadoc of the MongoDB Java Driver, it says :

DBCursor.count(): Counts the number of objects matching the query. This does not take limit/skip into consideration.

DBCursor.size(): Counts the number of objects matching the query. This does take limit/skip into consideration.




回答2:


More than an answer I'd like to point out an issue that our team faced "mixing" this two.

We had something like this:

DBCursor cursor = collection.find(query).limit(batchSize);

logger.info("{} items found.", cursor.count());

while (cursor.hasNext()) {
...
}

It turned out that after calling the cursor.count() method, the limit was ignored (plase take a look at this other question) , we intended to know how many items were returned by the query so we should have called the cursor.size() method instead, since calling the count one did have an undesired collateral effect.

I hope this could be helpful to anyone else since it was not that easy to find the source of the issue we were facing.



来源:https://stackoverflow.com/questions/11881347/difference-between-cursor-count-and-cursor-size-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!