Learn commands in MongoDB get data size of database and storage size of database using db.stats(). MongoDB get index size and index storage size of a database using db.stats(), different aspects of it so learn step by step.
1. Overview of MongoDB get Data size and storage size of Database
First of all, you need to understand why you required MongoDB data size or MongoDB disk storage size for selected database or index size or index storage size on disk for the selected database. This is needed from time to time not only for admin but also for developers of MongoDB and getting data size or disk storage size. There is a different approach to it but I will discuss all aspects here.
when you logged-in in mongos console or mongo shell terminal 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;
2. Data size for what?
In this section, you find different aspects of getting 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 database in MongoDB
- Get Data storage size of a database in MongoDB
- Get Index size of a database in MongoDB
- Get Index storage size on disk of a database in MongoDB
3. Overview of db.stats() command
In this section, you will learn about db.stats()
MongoDB command or mongoDB query. MongoDB get data size using db.stats()
of a database.
Following the tabular grid is helpful to understand different 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. |
By default db.stats()
returns in bytes, you can provide the scaling factor in stats() function:
– for bytes: no need to pass any scaling factor.
– for KB: 1024,
– for MB: 1024*1024,
– for GB: 1024*1024*1024
db.stats()
returns data size , data storage size on disk, average object size , no of collection in the database.
[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 }
4. MongoDB Get data size and get index size in bytes
Following query return the MongoDB data Size, mongodb storage size, no of indexes, index size, and storageSize are in bytes. I have not passed any scaling factor in db.stats()
function so it returns in bytes only. either you pass required scaling factor or you calculate by dividing 1024 for next level of unit.
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 }
5. Get data size and index size in Kilobytes (KB)
In this db.stats()
function, I passed scaling factor 1024 value to return value of dataSize, storageSize, indexSize in KB. Don’t forget to use select your database by using command use orderDB;
, else you get test database details.
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 }
6. Get data size and index size in MB (Megabytes)
In this db.stats()
function, I passed scaling factor 1024*1024
value to return value of dataSize, storageSize, indexSize in MB (megabytes). Don’t forget to use select your database by using command use orderDB;
, else you get test database details.
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 }
7. Get data size and index size in GB (Gigabytes)
In this db.stats()
function, I passed scaling factor 1024*1024*1024
to return respective values of dataSize, storageSize, indexSize in GB (gigabytes). Don’t forget to use select your database by using command use orderDB;
, else you get test database details.
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>
8. Get storage size for all dbs
Get storage size of all database by show dbs;
by running this on mongo shell terminal.
> show dbs; admin 0.078GB local 10.073GB test 0.078GB training 0.453GB
9. Storage size of all data
You can use linux du
command to know the storage size of all database.
[root@ranjeetJha~]# du -sh /var/lib/mongo/ 117G /var/lib/mongo/
10. Reference
Visit docs.mongodb.com for more details.
I hope you enjoyed this post about: how to get data size of a database in MongoDB, how to get storage data size of a database in MongoDB, MongoDB get index size of a database, and you can visit MongoDB tutorial for more articles.
Happy learning! 🙂