

- 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
Find the MongoDB collection size for name “Chris”
For this, use bsonsize() in MongoDB. Let us create a collection with documents −
> db.demo250.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd501627c0c63e7dba70") } > db.demo250.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd531627c0c63e7dba71") } > db.demo250.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd561627c0c63e7dba72") } > db.demo250.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd5b1627c0c63e7dba73") }
Display all documents from a collection with the help of find() method −
> db.demo250.find();
This will produce the following output −
{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" } { "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" } { "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" : "David" } { "_id" : ObjectId("5e46bd5b1627c0c63e7dba73"), "Name" : "Chris" }
Following is the query to find the MongoDB collection size for name “Chris” −
> var t = 0; > db.demo250.find({Name:"Chris"}).forEach(function(doc) { ... var s = Object.bsonsize(doc); ... t += s; ... }) > print(t);
This will produce the following output in bytes −
76
- Related Questions & Answers
- Change collection name in MongoDB?
- What is the MongoDB Capped Collection maximum allowable size?
- Renaming column name in a MongoDB collection?
- Looping MongoDB collection for sorting
- Updating MongoDB collection for _id?
- Get the size of a collection in MongoDB using conditions?
- Variable collection name in MongoDB shell with JavaScript?
- Find maximum score for duplicate Name values in MongoDB?
- Can we use the “.” symbol in MongoDB collection name?
- Find largest document size in MongoDB?
- Find oldest/ youngest post in MongoDB collection?
- Get execution stats in MongoDB for a collection
- How can I drop a collection in MongoDB with two dashes in the name?
- MongoDB query to find last object in collection?
- MongoDB query for capped sub-collection in an array
Advertisements