MongoDB – get data size and index size

Connect with

MongoDB


when you logged-in in mongos console and run the db.stats() command you can find with all values as 0 (Zero) , it means you have not selected any database. First select database using following

> use orderDB;

1. Overview of db.stats() command

Here are the details of db.stats() command. which are helpful to understand you what are the paremeter you get and what are the meaning of those parameters.

[table “” not found /]
[ranjeet@ranjeetJha ~]$ mongo
MongoDB shell version: 3.0.14
connecting to: test
rs1:SECONDARY> db.stats();
{
        "db" : "test",
        "collections" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}

2. Get data size and index size in bytes

In the following command dataSize and storageSize are in bytes.

rs1:SECONDARY> use orderDB
switched to db orderDB
rs1:SECONDARY> db.stats();
{
        "db" : "orderDB",
        "collections" : 153,
        "objects" : 62895870,
        "avgObjSize" : 3773.4611682452282,
        "dataSize" : 237335123088,
        "storageSize" : 46984601600,
        "numExtents" : 0,
        "indexes" : 248,
        "indexSize" : 1938391040,
        "ok" : 1
}

3. Get data size and index size in Kilobytes (KB)

rs1:SECONDARY> db.stats(1024);
{
        "db" : "orderDB",
        "collections" : 153,
        "objects" : 62895964,
        "avgObjSize" : 3773.464072877554,
        "dataSize" : 231773105.9404297,
        "storageSize" : 45883408,
        "numExtents" : 0,
        "indexes" : 248,
        "indexSize" : 1892960,
        "ok" : 1
}

4. Get data size and index size in MB (Megabytes)

rs1:SECONDARY> db.stats(1024*1024);
{
        "db" : "orderDB",
        "collections" : 153,
        "objects" : 62896034,
        "avgObjSize" : 3773.469466532659,
        "dataSize" : 226341.49919986725,
        "storageSize" : 44808.03515625,
        "numExtents" : 0,
        "indexes" : 248,
        "indexSize" : 1848.609375,
        "ok" : 1
}

5. Get data size and index size in GB (Gigabytes)

rs1:SECONDARY> db.stats(1024*1024*1024);
{
        "db" : "orderDB",
        "collections" : 153,
        "objects" : 62896116,
        "avgObjSize" : 3773.4681172999617,
        "dataSize" : 221.03682945296168,
        "storageSize" : 43.75809097290039,
        "numExtents" : 0,
        "indexes" : 248,
        "indexSize" : 1.8053054809570312,
        "ok" : 1
}

rs1:SECONDARY>

Your comments are welcome to improve this post, you have learned , how to get size of index and data from a particular database in mongodb. Happy learning.


Connect with

Leave a Reply

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