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

In MongoDB, count() and find().count() both return the number of documents in a collection, but they have subtle differences in how they handle query conditions and performance characteristics.

Syntax

db.collection.count(query, options)
db.collection.find(query).count()

Sample Data

db.countDemo.insertMany([
    {"UserId": 1, "UserName": "John"},
    {"UserId": 2, "UserName": "Carol"},
    {"UserId": 3, "UserName": "Bob"},
    {"UserId": 4, "UserName": "Mike"}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5c7f9d278d10a061296a3c5d"),
        ObjectId("5c7f9d308d10a061296a3c5e"),
        ObjectId("5c7f9d3a8d10a061296a3c5f"),
        ObjectId("5c7f9d428d10a061296a3c60")
    ]
}

Method 1: Using count()

Count all documents in the collection ?

db.countDemo.count();
4

Method 2: Using find().count()

Count documents using find() chained with count() ?

db.countDemo.find().count();
4

Key Differences

  • count() is a direct collection method that uses metadata for faster counting
  • find().count() creates a cursor first, then counts the results
  • Both methods are deprecated in MongoDB 4.0+ in favor of countDocuments() and estimatedDocumentCount()
  • Performance-wise, count() is typically faster for simple counts without complex queries

Conclusion

While count() and find().count() produce identical results for basic counting, count() is generally more efficient. Modern MongoDB versions recommend using countDocuments() for accurate counts and estimatedDocumentCount() for performance.

Updated on: 2026-03-15T00:06:43+05:30

459 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements