Open In App

MongoDB – countDocuments() Method

Last Updated : 17 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

MongoDB provides various methods to work with documents in a collection and one such method is countDocuments(). This method is particularly useful when we need to count the number of documents that match a specified query filter. In this article, we will learn about the MongoDB countDocuments by understanding the various examples in detail.

MongoDB countDocuments()

The countDocuments() method in MongoDB is used to count the number of documents in a collection that match a specified query filter. It provides an accurate count by executing a full collection scan or using indexes to fulfill the query filter.

It takes two arguments first one is the selection criteria and the other is optional. 

  • This method does not use metadata to return the count. It performs aggregation of the documents and returns a precise count.
  • We are allowed to use this method in multi-document transactions.
  • This method returns 0 for an empty collection or if the given collection is not in the database.
  • In this method, we are not allowed to use $where, $near and $nearSphere operators as a part of the query expressions. 

Syntax

db.collection.countDocuments(query, options)

Parameters

  • query: A document that specifies the selection criteria using query operators.
  • options (Optional): An object that specifies additional options for the count operation. Some of the options include:
    • limit: The maximum number of documents to count.
    • skip: The number of documents to skip before counting.
    • hint: An index hint to use for the query.
    • maxTimeMS: The maximum amount of time to allow the query to run.

Examples of MongoDB countDocuments

To understand MongoDB countDocuments() we need a collection called student on which we will perform various operations and queries.

Output:

studentsColl

Example 1: Count all Documents in a Collection

db.student.countDocuments({})

Output:

ExampleFirst

Explanation: Here, we are counting the total number of documents present in the student collection.

Example 2: Count all Documents that Match a Query

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

Output:

ExampleSec

Explanation: This query counts the number of documents in the `student` collection where the `age` field is greater than 18.

Conclusion

The countDocuments() method in MongoDB is a powerful and precise tool for counting documents within a collection based on specified criteria. Its ability to perform accurate counts, along with support for various query parameters, makes it an invaluable method for database operations.

FAQs on MongoDB countDocuments()

Can the countDocuments() method use indexes to fulfill the query filter?

Yes, the countDocuments() method can use indexes to fulfill the query filter, which can help optimize performance.

What will the countDocuments() method return if the collection is empty or does not exist in the database?

If the collection is empty or does not exist in the database, the countDocuments() method will return 0.

Are there any query operators that cannot be used with the countDocuments() method?

Yes, the countDocuments() method does not support the $where, $near, and $nearSphere operators as part of the query expressions.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: