MongoDB中count()和find().count()之间的区别 [英] Difference between count() and find().count() in MongoDB

查看:259
本文介绍了MongoDB中count()和find().count()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间的区别是什么,我基本上想在mycollection中找到所有文档. db.mycollection.count()db.mycollection.find().count()?

What is the difference between, I basically wanted to find all the documents in the mycollection. db.mycollection.count() vs db.mycollection.find().count()?

它们都返回相同的结果.为什么有人会选择count()find().count()?与find()应用默认限制(如果我错了,请纠正我)相反,您必须键入"it"才能在shell中查看更多内容.

They both returns the same result. Is there any reason why would somebody choose the count() vs the find().count()? In contrast to the fact that find() has a default limit applied (correct me if I'm wrong) to which you would have to type "it" in order to see more in the shell.

推荐答案

db.collection.count()cursor.count()只是分片群集中的错误.

db.collection.count() and cursor.count() are simply wrappers around the count command thus running db.collection.count() and cursor.count() with/without the same same will return the same query argument, will return the same the result. However the count result can be inaccurate in sharded cluster.

与4.0功能兼容的MongoDB驱动程序已弃用 各自的游标和集合count()API,而推荐使用新的API countDocuments()和estimatedDocumentCount().对于特定的API 给定驱动程序的名称,请参阅驱动程序文档.

MongoDB drivers compatible with the 4.0 features deprecate their respective cursor and collection count() APIs in favor of new APIs for countDocuments() and estimatedDocumentCount(). For the specific API names for a given driver, see the driver documentation.

db.collection.countDocuments 方法在内部使用聚合查询返回文档计数,而 db.collection.estimatedDocumentCount/ 返回基于元数据的文档计数

The db.collection.countDocuments method internally uses an aggregation query to return the document count while db.collection.estimatedDocumentCount/ returns documents count based on metadata.

值得一提的是,如estimatedDocumentCount输出可能不准确. ="noreferrer">文档.

It worth mentioning that the estimatedDocumentCount output can be inaccurate as mentioned in the documentation.

这篇关于MongoDB中count()和find().count()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆