
- 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 real document BSON size in MongoDB?
You can use Object.bsonsize() to get real document size. It prints the BSON size of a document in bytes. Let us create a collection with documents −
> db.demo477.insertOne({"ClientId":1,"ClientName":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e82015fb0f3fa88e227908f") } > db.demo477.insertOne({"ClientId":2,"ClientName":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e820167b0f3fa88e2279090") } > db.demo477.insertOne({"ClientId":3,"ClientName":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e82016db0f3fa88e2279091") }
Display all documents from a collection with the help of find() method −
> db.demo477.find();
This will produce the following output −
{ "_id" : ObjectId("5e82015fb0f3fa88e227908f"), "ClientId" : 1, "ClientName" : "Chris" } { "_id" : ObjectId("5e820167b0f3fa88e2279090"), "ClientId" : 2, "ClientName" : "David" } { "_id" : ObjectId("5e82016db0f3fa88e2279091"), "ClientId" : 3, "ClientName" : "Bob" }
Following is the query to get the BSON size of a document in bytes −
> Object.bsonsize(db.demo477.findOne())
This will produce the following output −
62
- Related Articles
- Find largest document size in MongoDB?
- What is the maximum size of a document in MongoDB?
- Get database data size in MongoDB?
- Get array items inside a MongoDB document?
- MongoDB query to get last inserted document?
- Get the top most document from a MongoDB collection
- Get the storage size of a database in MongoDB?
- How to get embedded data in a MongoDB document?
- How to get the matching document inside an array in MongoDB?
- Get the size of a collection in MongoDB using conditions?
- Get the size of all the documents in a MongoDB query?
- How to get distinct list of sub-document field values in MongoDB?
- In MongoDB, what is the most efficient way to get the first and last document?
- MongoDB Aggregate to get average from document and of array elements?
- What is the BSON query for the command 'show dbs' (list of databases) in MongoDB?

Advertisements