Count the number of Documents in MongoDB using Python
Mongodb is a document-oriented NoSQL database which is non-relational DB. Mongodb is a schema-free database which is based on Binary JSON format. It is organized with a group of documents (rows in RDBMS) called collection (table in RDBMS). The collections in mongodb are schema-less.
PyMongo is one of the MongoDB drivers or client libraries. Using the PyMongo module we can send requests and receive responses from
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Count the number of Documents using Python
Method 1: Using count()
The total number of documents present in the collection can be retrieved by using
Example : Count the number of documents (my_data) in the collection using
The number of documents in collection : 8
Method 2: count_documents()
Alternatively, you can also use count_documents() function in pymongo to count the number of documents present in the collection.
Example : Retrieves the documents present in the collection and the count of the documents using
Total number of documents : 8