MongoDB collection data size

Connect with

MongoDBLearn how to get MongoDB collection data size, storage data size MongoDB collection using db.collection.stats(). Get Document size of MongoDB using Object.bsonsize({}), MognoDB collection index size using db.collection.stats(), different aspects of it so learn step by step.

1. MongoDB collection data size

In this section, you find different aspects of getting MongoDB collection data size. Here, data size and storage size can be different if you have used any compression algorithm to store the data. In my case, I have used snappy compression which compresses 5 times of your data, which means if your data size is 5MB then your storage size will be 1MB.

  • Get Data size of a Collection in MongoDB
  • Get Data storage size of a Collection in MongoDB
  • Get Index size of a Collection in MongoDB

2. Calculate size of document(s)

In this section, you learn about how to get size of document in a collection in mongoDB using Object.bsonsize({}) for matching condition of a query
Syntax:

> Object.bsonsize({username:"user101"})

Output:

> Object.bsonsize({username:"user101"})
27
> Object.bsonsize({_id:""+ObjectId()})
39
> Object.bsonsize({_id:ObjectId()})
22
> Object.bsonsize(db.user.find())
709
> Object.bsonsize(db.user.findOne())
85

3. Overview of parameter for db.collection.stats()

For understanding a different aspect of MongoDB collection data size you must understand the parameters details.

key details
dbthis is name of the database which you have selected.
collections self descriptive
avgObjSizeself descriptive, average size of the object in this database
dataSize size of the data in bytes/KB/MB/GB\, based upon the factor provided (i.e 1024)
storageSize,size of the index in bytes/KB/MB/GB\, based upon the factor provided (i.e 1024)
indexSizesize of the index in bytes/KB/MB/GB\, based upon the factor provided (i.e 1024)
indexesself descriptive \, no of index in this database (i.e. orderDB)
fileSizeself descriptive\, size of the file which hold select database
okthe value of 1 means , command runs successfully if 0 then something went wrong.

4. Getting size of index and data on a collection

if you not provided any scaling factor in function db.collection.stats(); , it returns in bytes. you can provide the scaling factor to calculate index size and data size in KB, MB and GB respectively:
By default db.collection.stats() returns in bytes, you can provide the scaling factor in db.collection.stats() function:
– for bytes: no need to pass any scaling factor.
– for KB: 1024,
– for MB: 1024*1024,
– for GB: 1024*1024*1024
Syntax

> db.collection.stats();

Output

> use training;
switched to db training
> show collections;
system.indexes
user
> db.user.stats();
{
        "ns" : "training.user",
        "count" : 1000000,
        "size" : 112000000,
        "avgObjSize" : 112,
        "storageSize" : 174735360,
        "numExtents" : 12,
        "nindexes" : 1,
        "lastExtentSize" : 50798592,
        "paddingFactor" : 1,
        "systemFlags" : 1,
        "userFlags" : 1,
        "totalIndexSize" : 32458720,
        "indexSizes" : {
                "_id_" : 32458720
        },
        "ok" : 1
}
>

5. Get size of index and data in MongoDB collection with scaling factor

db.user.stats(1024), is used to get size of data and size of index on user collection in MongoDB.
– for bytes: no need to pass any scaling factor.
– for KB: 1024,
– for MB: 1024*1024,
– for GB: 1024*1024*1024
For KB (kilobytes)

db.user.stats(1024)

for MB:

 db.user.stats(1024*1024);

for GB:

 db.user.stats(1024*1024*1024);

for TB:

 db.user.stats(1024*1024*1024*1024);

6. Get size of index only

It returns the size of the index only in bytes.

db.user.totalIndexSize();

You can get size of data for a database in mongoDB

7. Reference

Visit docs.mongodb.com for more details.
I hope you enjoyed this post about: how to get data size of a collection in MongoDB, how to get storage data size of a collection in MongoDB, MongoDB get index size of a Collection, and you can visit MongoDB tutorial for more articles.

Happy learning! 🙂
Your suggestions are welcome to improve the post.


Connect with

2 thoughts on “MongoDB collection data size”

Leave a Reply to suman Cancel Reply

Your email address will not be published. Required fields are marked *