Get database data size in MongoDB?

To get the database data size in MongoDB, you can use the db.stats() method. This method returns detailed statistics about the current database including data size, storage size, and other useful metrics.

Syntax

db.stats()

Example 1: Check Current Database Statistics

First, check which database you are currently using ?

db
test

Now get the database statistics for the 'test' database ?

db.stats()
{
    "db" : "test",
    "collections" : 114,
    "views" : 0,
    "objects" : 391,
    "avgObjSize" : 83.0076726342711,
    "dataSize" : 32456,
    "storageSize" : 3211264,
    "numExtents" : 0,
    "indexes" : 120,
    "indexSize" : 2494464,
    "fsUsedSize" : 126172377088,
    "fsTotalSize" : 199229435904,
    "ok" : 1
}

Example 2: Switch Database and Check Statistics

Switch to a different database called 'sample' ?

use sample
switched to db sample

Verify the current database ?

db
sample

Get statistics for the 'sample' database ?

db.stats()
{
    "db" : "sample",
    "collections" : 25,
    "views" : 0,
    "objects" : 67,
    "avgObjSize" : 85.32835820895522,
    "dataSize" : 5717,
    "storageSize" : 626688,
    "numExtents" : 0,
    "indexes" : 24,
    "indexSize" : 610304,
    "fsUsedSize" : 126173016064,
    "fsTotalSize" : 199229435904,
    "ok" : 1
}

Key Statistics Explained

  • dataSize − Actual size of data in bytes
  • storageSize − Total storage allocated for collections
  • indexSize − Total size of all indexes
  • objects − Total number of documents
  • collections − Number of collections in the database

Conclusion

The db.stats() method provides comprehensive database statistics including data size, storage usage, and collection counts. Use dataSize for actual data volume and storageSize for total allocated space.

Updated on: 2026-03-15T00:18:20+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements