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.

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.

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.

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.