

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of rows in a MongoDB collection
To count number of documents, use count() in MongoDB. Let us create a collection with documents −
> db.demo664.insertOne({_id:1,ClientName:"Chris"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo664.insertOne({_id:2,ClientName:"Bob"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo664.insertOne({_id:3,ClientName:"Sam"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo664.insertOne({_id:4,ClientName:"David"}); { "acknowledged" : true, "insertedId" : 4 }
Display all documents from a collection with the help of find() method −
> db.demo664.find();
This will produce the following output −
{ "_id" : 1, "ClientName" : "Chris" } { "_id" : 2, "ClientName" : "Bob" } { "_id" : 3, "ClientName" : "Sam" } { "_id" : 4, "ClientName" : "David" }
Following is the query to count number of rows −
> var numberOfRows=db.demo664.count(); > print ("Number Of Rows="+numberOfRows);
This will produce the following output −
Number Of Rows=4
- Related Questions & Answers
- How to count the number of documents in a MongoDB collection?
- Get the count of the number of documents in a MongoDB Collection?
- Count number of documents from MongoDB collection inside Array?
- Limit the number of documents in a collection in MongoDB?
- MongoDB - How to copy rows into a newly created collection?
- Count number of rows in each table in MySQL?
- How to count number of rows in a table with jQuery?
- How to count number of keys in a MongoDB document?
- Clone a collection in MongoDB?
- Search array of objects in a MongoDB collection?
- Fastest way to count number of rows in MySQL table?
- Python Pandas - Count the number of rows in each group
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- Count number of elements in an array with MongoDB?
- Is there a way to limit the number of records in a certain MongoDB collection?
Advertisements