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.
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.
count()
Method
limit
, skip
, and hint
for more refined querieaggregate()
instead for accurate results.db.collection.aggregate()
method instead.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:
Parameters ofdb.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()
count()
Method 1. Selection Criteria
count()
will count all documents in the collection.The second parameter is an optional document that allows fine-tuning of the counting process.
Parameter Descriptionlimit
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.
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:
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.
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:
$gt
stands for "greater than" and is used to filter documents where the age
field is greater than 18
.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:
skip: 1
→ Skips the first document in the collection.limit: 2
→ Counts only the next two documents after skipping.count()
in MongoDB 1. Avoid Using count()
in Transactions
count()
method cannot be used within transactions in MongoDB.aggregate()
method insteadcountDocuments()
Instead of count()
count()
method is deprecated in MongoDB 4.0+.countDocuments()
, which provides more accurate results in sharded clusters and distributed environments.hint
parameter allows MongoDB to leverage an index for faster count queries.Example:
db.student.count({ age: { $gt: 18 } }, { hint: { age: 1 } })
age
field ({ age: 1 }
) to perform an optimized count operation.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