

- 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
Get number of records in MongoDB?
To get number of records, use count() in MongoDB. Let us create a collection with documents −
> db.demo697.insertOne({Name:"Chris",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d7d1551299a9f98c9395") } > db.demo697.insertOne({Name:"Bob",Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d7d8551299a9f98c9396") } > db.demo697.insertOne({Name:"David",Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d7dd551299a9f98c9397") }
Display all documents from a collection with the help of find() method −
> db.demo697.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6d7d1551299a9f98c9395"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea6d7d8551299a9f98c9396"), "Name" : "Bob", "Age" : 23 } { "_id" : ObjectId("5ea6d7dd551299a9f98c9397"), "Name" : "David", "Age" : 24 }
Following is the query to get number of records −
> var numberOfRecords= db.demo697.count(); > print("Number of records=",numberOfRecords);
This will produce the following output −
Number of records= 3
- Related Questions & Answers
- MongoDB Aggregate to limit the number of records
- Get number of updated documents in MongoDB?
- Get the number of open connections in MongoDB?
- How to get the last N records in MongoDB?
- MongoDB query to get date records in a range
- How to get the number of records in a table using JDBC?
- MongoDB query to get a specific number of items
- Find duplicate records in MongoDB?
- Find a specified amount of records in MongoDB?
- Get the count of the number of documents in a MongoDB Collection?
- Get the maximum mark records from a collection with documents in MongoDB
- MongoDB query to get only a specific number of elements
- Is there a way to limit the number of records in a certain MongoDB collection?
- Deleting all records of a collection in MongoDB Shell?
- Update tag records in MongoDB quickly
Advertisements