A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mongodb/mongodb-count-method-db-collection-count/ below:

MongoDB Count() Method - db.Collection.count()

MongoDB Count() Method - db.Collection.count()

Last Updated : 23 Jul, 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 Count() 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: <integer>,
    skip: <integer>,
    hint: <string or document>,
    maxTimeMS : <integer>,
    readConcern: <string>,
    collation: <document>  
})

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:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4