Abhiram mishra
Abhiram mishra

Reputation: 1617

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

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

They both return the same result. Is there any reason why somebody would 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.

Upvotes: 49

Views: 119581

Answers (5)

Sede
Sede

Reputation: 61293

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.

Upvotes: 49

Amitesh Bharti
Amitesh Bharti

Reputation: 15785

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.

Upvotes: 4

Gil Beyruth
Gil Beyruth

Reputation: 608

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.

Upvotes: 1

makhdumi
makhdumi

Reputation: 1328

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

Upvotes: 4

sheilak
sheilak

Reputation: 5873

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

Upvotes: 22

Related Questions