
- 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 the size of all the documents in a MongoDB query?
To get the size of all the documents in a query, you need to loop through documents. Let us first create a collection with documents −
> db.sizeOfAllDocumentsDemo.insertOne({"StudentFirstName":"John","StudentSubject":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3c0c1edc6604c74817cd7") } > db.sizeOfAllDocumentsDemo.insertOne({"StudentFirstName":"Larry","StudentSubject":["MySQL","PHP"],"StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3c0d9edc6604c74817cd8") } > db.sizeOfAllDocumentsDemo.insertOne({"StudentFirstName":"Chris","StudentSubject":["SQL Server","C#"],"StudentAge":23,"StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3c0fbedc6604c74817cd9") }
Following is the query to display all documents from a collection with the help of find() method −
> db.sizeOfAllDocumentsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd3c0c1edc6604c74817cd7"), "StudentFirstName" : "John", "StudentSubject" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5cd3c0d9edc6604c74817cd8"), "StudentFirstName" : "Larry", "StudentSubject" : [ "MySQL", "PHP" ], "StudentAge" : 21 } { "_id" : ObjectId("5cd3c0fbedc6604c74817cd9"), "StudentFirstName" : "Chris", "StudentSubject" : [ "SQL Server", "C#" ], "StudentAge" : 23, "StudentCountryName" : "US" }
Following is the query to get the size of all the documents in a query −
> var allDocument = db.sizeOfAllDocumentsDemo.find(); > var counter = 0; > allDocument.forEach( ... function(myDocument) { ... counter = counter+Object.bsonsize(myDocument) ... } ... ); > print(counter);
This will produce the following output −
358
- Related Articles
- MongoDB Group query to get the count of repeated marks in documents?
- MongoDB query for counting the distinct values across all documents?
- Get the maximum mark records from a collection with documents in MongoDB query
- MongoDB query to update all documents matching specific IDs
- MongoDB query to get distinct FirstName values from documents
- Query for documents where array size is greater than 1 in MongoDB?
- MongoDB inverse of query to return all items except specific documents?
- Fetch all the ids of MongoDB documents?
- MongoDB - Query embedded documents?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to get documents with multiple conditions set in $or?
- Get the count of the number of documents in a MongoDB Collection?
- Get all the MongoDB documents but not the ones with two given criteria’s?
- Get the storage size of a database in MongoDB?

Advertisements