Count the Number of Documents in a Collection

Learn via video courses
Topics Covered

Overview

The db.collection.count() method is used to count the number of documents that matches the find query for a collection or view. db.collection.count() can be used to count the documents that match single or multiple criteria. db.collection.countDocuments() method is also used to return the documents count that matches the query for the specified collection or view. But db.collection.countDocuments() does not rely on metadata and db.collection.count() rely on metadata.

Introduction

MongoDB provides various methods to the developers for counting documents in a collection or views without any condition or based on some particular conditions like filtering based on the value of a field or query. These methods are very important for querying the database, and these make a MongoDB powerful tool for retrieving and storing data. db.collection.count(), db.collection. court documents (), db.collection.estimatedDocumentCount(), cursor. count(), and sortByCount() are the MongoDB methods for counting the documents in a collection or view. Every method has its working and speciality.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

db.collection.count() Method

The db.collection.count() method returns the document count of those documents that would match the find() query for the view or collection. Her collection is the collection or view name. A numeric value is simply returned by it which represents the matched documents count.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Use Cases

First of all, we will create a collection with the name Student and then insert some data in this collection in the form Student_id, Student_name, Student_dob, and Student_Address.

Count All Documents From the Collection

Now if we want to count all the documents all the collection then we need to write the query given below

Output

It will return 3 as output as there is a total of 3 documents in the collection Student.

Count all Documents that Match a Query

If we want to count the number of documents having "student_address":"Ghaziabad". Then we need to write the query given below:

Output

It will return 2 as output as there are two documents that have student_address equal to Ghaziabad.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Count all Documents that Match a Query Using More Than on Criteria

Count the number of the documents in the collection Student filtering with the field student_address equal to Ghaziabad and student_id is 1:

Output

It will return 1 as output as there is only one document that has student_address equal to Ghaziabad and student_id equal to 1.

db.collection.countDocuments()

db.collection.countDocuments() method is used to return the documents count that matches the query for the specified collection or view. db.collection.count() works on the metadata, so sometimes it may return an approximate count of the matched documents. But db.collection.countDocuments() does not work on metadata, and an accurate count of matched documents is returned by it by performing the document aggregation.

db.collection.estimatedDocumentCount() Method

db.collection.estimatedDocumentCount() method of the MongoDB return the all documents count of a collection or view and it is a wrapper fo the mongodb count command. Query filter is not taken by this method, in place metadata is used by it for returning the document count of the whole collection or view.

cursor.count() Method

The cursor.count() method is used to count the number of documents referenced by the cursor. The actual query is not performed by it. It simply counts and returns that count as an output.

sortByCount()

$sortByCount is an aggregation operator by which the incoming documents are grouped based on some specified expression value, and then the count of matched documents is computed in every distinct group.

Conclusion

  • MongoDB provides various methods to the developers for counting documents in a collection or view without any condition or based on some particular conditions like filtering based on the value of a field or query.
  • The db.collection.count() method returns the document count of those documents that would match the find() query for the view or collection.
  • db.collection.count() can be used to count the documents that match single or multiple criteria.
  • db.collection.countDocuments() method is used to return the documents count that matches the query for the specified collection or view.
  • db.collection.estimatedDocumentCount() method of the MongoDB returns the all document count of a collection or view.
  • cursor.count() method is used to count the number of documents referenced by the cursor. The actual query is not performed by it.
  • $sortByCount is an aggregation operator by which the incoming documents are grouped based on some specified expression value, and then the count of matched documents is computed in every distinct group.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more