
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Get a count of total documents with MongoDB while using limit?
You can use $facet operator for this. To understand the concept, let us create a collection with document. The query to create a collection with document is as follows −
> db.totalDocumentDemo.insertOne({"InstructorId":100,"InstructorName":"Larry","InstructorFav ouriteSubject":["Java","MongoDB","Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76e6701e9c5dd6f1f78274") } > db.totalDocumentDemo.insertOne({"InstructorId":200,"InstructorName":"Sam","InstructorFav ouriteSubject":["SQL Server","C#","Javascript"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76e69c1e9c5dd6f1f78275") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.totalDocumentDemo.find().pretty();
Output
{ "_id" : ObjectId("5c76e6701e9c5dd6f1f78274"), "InstructorId" : 100, "InstructorName" : "Larry", "InstructorFavouriteSubject" : [ "Java", "MongoDB", "Python" ] } { "_id" : ObjectId("5c76e69c1e9c5dd6f1f78275"), "InstructorId" : 200, "InstructorName" : "Sam", "InstructorFavouriteSubject" : [ "SQL Server", "C#", "Javascript" ] }
Here is the query to get a count of total documents from a collection −
> db.totalDocumentDemo.aggregate([ ... { ... $facet:{ ... Alldata:[{$match:{}}], ... totalDocument: [{ $count: 'totalDocument' }] ... } ... } ... ]).pretty();
The following is the output displaying the count of documents −
Output
{ "Alldata" : [ { "_id" : ObjectId("5c76e6701e9c5dd6f1f78274"), "InstructorId" : 100, "InstructorName" : "Larry", "InstructorFavouriteSubject" : [ "Java", "MongoDB", "Python" ] }, { "_id" : ObjectId("5c76e69c1e9c5dd6f1f78275"), "InstructorId" : 200, "InstructorName" : "Sam", "InstructorFavouriteSubject" : [ "SQL Server", "C#", "Javascript" ] } ], "totalDocument" : [ { "totalDocument" : 2 } ] }
- Related Articles
- Get total number of rows while using LIMIT in MySQL?
- Get the count of the number of documents in a MongoDB Collection?
- Get count of array elements from a specific field in MongoDB documents?
- Limit the number of documents in a collection in MongoDB?
- MongoDB Group query to get the count of repeated marks in documents?
- How to get items with a specific value from documents using MongoDB shell?
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- How to skip documents while retrieving data from MongoDB, using java?
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- Get MongoDB documents with max attribute per group in a collection?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Get number of updated documents in MongoDB?
- How to aggregate sum in MongoDB to get the total count?
- MongoDB query to insert but limit the total records
- How to count the number of documents in a MongoDB collection?

Advertisements