Getting the size of the data and size of the index in MongoDB is very easy by using commands i.e. db.stats(). Let us go through one by one in this article.
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 parameter you get and what are the meaning of those parameters.
key | details |
---|---|
db | this is name of the database which you have selected. |
collections | self descriptive |
avgObjSize | self 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) |
indexSize | size of the index in bytes/KB/MB/GB\, based upon the factor provided (i.e 1024) |
indexes | self descriptive \, no of index in this database (i.e. orderDB) |
fileSize | self descriptive\, size of the file which hold select database |
ok | the value of 1 means , command runs successfully if 0 then something went wrong. |
[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 the size of the index and data from a particular database in MongoDB. Happy learning.