MongoDB 中 count() 和 find().count() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32666330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:32:30  来源:igfitidea点击:

Difference between count() and find().count() in MongoDB

mongodbmongodb-query

提问by Abhiram mishra

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

有什么区别,我基本上是想把mycollection里面的所有文档都找一下。 db.mycollection.count()db.mycollection.find().count()?

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.

它们都返回相同的结果。有什么理由为什么有人会选择count()vsfind().count()吗?与find()应用默认限制的事实相反(如果我错了,请纠正我),您必须键入“它”才能在外壳中看到更多内容。

回答by styvane

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

db.collection.count()并且cursor.count()只是count命令的包装器,因此运行db.collection.count()cursor.count()使用/不使用相同将返回相同的查询参数,将返回相同的结果。然而,count结果在分片集群中可能不准确

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.

与 4.0 功能兼容的 MongoDB 驱动程序弃用了它们各自的游标和集合 count() API,转而支持 countDocuments() 和estimatedDocumentCount() 的新 API。有关给定驱动程序的特定 API 名称,请参阅驱动程序文档。

The db.collection.countDocumentsmethod internally uses an aggregation queryto return the document count while db.collection.estimatedDocumentCount/returns documents count based on metadata.

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

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

值得一提的是,estimatedDocumentCount文档中所述,输出可能不准确。

回答by sheilak

db.collection.count()without parameters counts all documents in a collection. db.collection.find()without parameters matches all documents in a collection, and appending count()counts them, so there is no difference.

db.collection.count()不带参数计算集合中的所有文档。db.collection.find()不带参数匹配集合中的所有文档,并追加count()计数它们,因此没有区别。

This is confirmed explicitly in the db.collection.count() documentation:

这在db.collection.count() 文档中得到了明确确认:

To count the number of all documents in the orders collection, use the following operation:

db.orders.count()

This operation is equivalent to the following:

db.orders.find().count()

要计算订单集合中所有文档的数量,请使用以下操作:

db.orders.count()

此操作等效于以下操作:

db.orders.find().count()

回答by makhdumi

As is mentioned in another answer by sheilak, the two are equivalent - except that db.collection.count()can be inaccurate for sharded clusters.

正如 sheilak 在另一个答案中提到的那样,两者是等效的 - 除了分片db.collection.count()集群可能不准确。

The latest documentationsays:

最新的文件说:

count() is equivalent to the db.collection.find(query).count() construct.

count() 等效于 db.collection.find(query).count() 构造。

And then,

进而,

Sharded Clusters

On a sharded cluster, db.collection.count() can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.

分片集群

在分片集群上,如果存在孤立文档或正在进行块迁移,db.collection.count() 可能会导致计数不准确。

The documentation explains how to mitigate this bug(use an aggregate).

该文档解释了如何缓解此错误(使用聚合)。

回答by Amitesh

db.collection.count() is equivalent to the db.collection.find(query).count() construct.

db.collection.count() 相当于 db.collection.find(query).count() 构造。

Examples

例子

Count all Documents in a Collection

计算集合中的所有文档

db.orders.count()

This operation is equivalent to the following:

此操作等效于以下操作:

db.orders.find().count()

Count all Documents that Match a Query

计算与查询匹配的所有文档

Count the number of the documents in the orders collection with the field ord_dt greater than new Date('01/01/2012'):

计算订单集合中 ord_dt 字段大于 new Date('01/01/2012') 的文档数:

db.orders.count( { ord_dt: { $gt: new Date('01/01/2012') } } )

The query is equivalent to the following:

该查询等效于以下内容:

db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).count()

As per the documentation in the following scenario db.collection.count() can be inaccurate :

根据以下场景中的文档 db.collection.count() 可能不准确:

  1. On a sharded cluster, db.collection.count() without a query predicate can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.
  2. After an unclean shutdown of a mongod using the Wired Tiger storage engine, count statistics reported by count() may be inaccurate.
  1. 在分片集群上,如果存在孤立文档或正在进行块迁移,没有查询谓词的 db.collection.count() 可能会导致计数不准确。
  2. 在使用 Wired Tiger 存储引擎不正常关闭 mongod 后,count() 报告的计数统计信息可能不准确。

回答by Gil Beyruth

I believe if you are using some kind of pagination like:

我相信如果您使用某种分页,例如:

find(query).limit().skip().count() 

You will not get the same result as

你不会得到相同的结果

count(query)

So in cases like this, if you want to get the total, I think you might have to use both.

因此,在这种情况下,如果您想获得总数,我认为您可能必须同时使用两者。