MongoDB Count() Method db.Collection.count() (original) (raw)

Last Updated : 04 Feb, 2025

MongoDB’s count() method is a powerful tool for **retrieving the number of documents in a collection that match a specified query. It offers flexibility in filtering and is useful for obtaining quick counts based on various criteria.

In this article, We will explain the **MongoDB count() method in detail, including **its syntax, parameters, examples, and best practices. By the end of this article, we will have a deep understanding of how to use the count() method effectively for different use cases.

**What is MongoDB C ount() Method?

The count() method in **MongoDB is a simple and effective way to count the number of documents that match a given query in a collection. It can take an optional query parameter to filter the documents before counting. It is functionally equivalent to db.collection.find().count() and is available in both db.collection and **cursor objects.

**Key Features of count() Method

**Syntax of count() Method

The count() method can be used in two ways:

1. **Count all documents in a collection:

db.Collection_Name.count()

**2. Count documents that match a filter condition:

db.Collection_Name.count(
Selection_criteria,
{
limit: ,
skip: ,
hint: ,
maxTimeMS : ,
readConcern: ,
collation:
})

Or if we want to count the number of documents in the collection

db.Collection_name.count()

**Parameters of count() Method

**1. Selection Criteria

**2. Optional Parameters

The second parameter is an **optional document that allows fine-tuning of the counting process.

Parameter Description
**limit Limits the number of documents counted.
**skip Skips a specified number of documents before counting.
**hint Specifies an index to use for performance optimization.
**maxTimeMS Sets the maximum time allowed for the query to execute.
**readConcern Defines the read concern level (e.g., majority).
**collation Allows language-specific sorting and case-sensitivity rules.

**Return Type

The count() method **returns an integer representing the number of documents that match the selection criteria.

**Examples of Using count() in MongoDB

To demonstrate the count() method, we will use a sample **MongoDB database named gfg with a **collection called student. The collection contains multiple documents, each representing a student with **name and **age fields. Below is a sample dataset used for the examples:

Example 1: Count all Documents in a Collection

This example demonstrates how to count the total number of documents present in the student collection.

**Query:

db.student.count()

**Output:

**Explanation:

The above query retrieves the total number of documents stored in the student collection. Since no filter condition is applied, it counts **all documents in the collection and returns the result as an integer.

Example 2: Count all Documents that Match a Query

This example demonstrates how to count documents in the student collection that meet a specific condition, such as **age greater than 18.

**Query:

db.student.count({age:{$gt:18}})

**Output:

Count-all-Documents-that-Match-a-Query

**Explanation:

**Example 3: Count with limit and skip Parameters

This example demonstrates how to **limit the number of documents counted while **skipping a specified number of documents in the student collection.

**Query:

db.student.count({}, { skip: 1, limit: 2 })

**Explanation:

**Best Practices for Using count() in MongoDB

**1. Avoid Using count() in Transactions

**2. Use countDocuments() Instead of count()

**3. Optimize Performance Using Indexes

**Example:

db.student.count({ age: { $gt: 18 } }, { hint: { age: 1 } })

Conclusion

The count() method in MongoDB is a useful tool for quickly determining the number of documents that meet certain criteria. It can be used with various options to fine-tune the counting process, such as limiting the number of documents counted or specifying a maximum time for the operation to complete. However, with its **deprecation in MongoDB 4.0+, it is best practice to **use countDocuments() for more accurate and efficient results.