

- 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 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 Questions & Answers
- Find largest document size in MongoDB?
- What is the maximum size of a document in MongoDB?
- Get database data size in MongoDB?
- Get the top most document from a MongoDB collection
- MongoDB query to get last inserted document?
- Get array items inside a MongoDB document?
- Get the storage size of a database in MongoDB?
- 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 embedded data in a MongoDB document?
- Python Get the real time currency exchange rate?
- In MongoDB, what is the most efficient way to get the first and last document?
- Updating nested document in MongoDB
- What is the BSON query for the command 'show dbs' (list of databases) in MongoDB?
Advertisements