47

What is the difference between, I basically wanted to find all the documents in the mycollection. db.mycollection.count() vs 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.

2
  • 7
    Type db.mycollection.count without the brackets into the shell and you will see the method calls in there with the last line being return this.find(query).count(); which basically says that unless you included special modifiers such as "skip" or "limit" then it is basically executing the same statement, even with a query. Even in the command form, there is essentiall no difference in the underlying execution. Sep 19, 2015 at 9:16
  • 3
    The shell truncates the results for convenience. The MongoDB API actually does not truncate anything. In other words: find() has no such limits. Sep 19, 2015 at 9:36

5 Answers 5

48

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 will return the same query argument, will return the same result. However the count result can be inaccurate in sharded cluster.

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.

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.

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

0
22

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.

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

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()

4

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

The latest documentation says:

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

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.

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

1
  • It is inaccurate even when the db isn't sharded if the document count is very high. Nov 4, 2018 at 19:36
4

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

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'):

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 :

  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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.