Count the Number of Documents in MongoDB using Python

To count the number of documents in a MongoDB collection, you can use the count_documents() method provided by the PyMongo library.

The count_documents() method takes an optional parameter which is the query document that specifies which documents to be counted. If we pass it as an empty document {}, the count_documents() method returns the total number of documents in the specified collection.

The count_documents() method has the following syntax:

db.collection.count_documents(filter, options)

For demonstration purpose, we have created a sample collection students under the school database and inserted 10 records into it using the insertMany() method of the MongoDB shell.

The below image shows that 10 records are inserted successfully into the students collection:

Count the number of documents in a MongoDB collection

The following python code counts the total number of documents in the students collection and prints the total count into the console:

from pymongo import MongoClient

# Connect to the database
mongoClient = MongoClient('mongodb://localhost:27017/')

# Access the school database
db = mongoClient['school']

# Count total records
total_records = db.students.count_documents({})

# Print the result
print('Total records are: ', total_records)

Output:

Total records are:  10

Count Documents that Match a Condition

You can also use the count_documents() method to count the documents that satisfy a given condition.

For demonstration purpose, let’s say that the student collection has the following documents:

{ "name": "arun", "marks": 76 },
{ "name": "rajesh", "marks": 87 },
{ "name": "moorthy", "marks": 54 },
{ "name": "raja", "marks": 73 },
{ "name": "usha", "marks": 97 },
{ "name": "priya", "marks": 49 },
{ "name": "Sundar", "marks": 75 },
{ "name": "Kavitha", "marks": 86 },
{ "name": "Dinesh", "marks": 92 },
{ "name": "Hema", "marks": 50 }

Now, we want to count only those students in the students collection that have marks greater than 75.

For that purpose, we can use the $gt operator and use the query {'marks':{'$gt': 75}} to count those records which have marks greater than 75.

from pymongo import MongoClient

# Connect to the database
mongoClient = MongoClient('mongodb://localhost:27017/')

# Access the school database
db = mongoClient['school']

# Count students having marks greater than 75
matched_records = db.students.count_documents({'marks':{'$gt': 75}})

# Print the result
print('The count of students having marks greater than 75 is: ', matched_records)

Output:

The count of students having marks greater than 75 is:  5

Get Estimated Count When the Collection is Large

When the size of the collection is very large, the count_documents() method can perform a bit slower. In such cases, you should use the estimated_document_count() method instead.

The estimated_document_count() method get an estimate of the total number of documents in a collection. It uses collection’s metadata to estimate the count, so it may not be 100% accurate, but it can be faster than count_documents() especially when the collection is large.

Syntax:

db.collectionName.estimated_document_count()

The following Python program gives the estimated count of the students collection:

from pymongo import MongoClient

# Connect to the database
mongoClient = MongoClient('mongodb://localhost:27017/')

# Access the school database
db = mongoClient['school']

# Get estimated count
estimated_count = db.students.estimated_document_count()

# Print the result
print('The estimated count of the collection is: ', estimated_count)

Output:

The estimated count of the collection is:  93216

Conclusion

In this article, we learned how we can count the total number of documents in a MongoDB collection.

In summary, you can use the count_documents() method of the PyMongo library of Python to get the total number of documents in a MongoDB collection.

If the size of the collection is very large, you can use the estimated_document_count() method to get an estimated count of total records in the collection.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.