- Reference >
- Database Commands >
- Diagnostic Commands >
- dbStats
dbStats¶
On this page
Definition¶
- 
dbStats¶
- The - dbStatscommand returns storage statistics for a given database. The command has the following syntax:- The - dbStatscommand takes the following fields:- Parameter - Description - dbStats- 1 - scale - Optional. The scale factor for the various size data. The - scaledefaults to 1 to return size data in bytes. To display kilobytes rather than bytes, specify a- scalevalue of- 1024.- If you specify a non-integer scale factor, MongoDB uses the integer part of the specified factor. For example, if you specify a scale factor of - 1023.999, MongoDB uses- 1023as the scale factor.- Starting in version 4.2, the output includes the - scaleFactorused to scale the size values.- In the - mongoshell, the- db.stats()function provides a wrapper around- dbStats.
Behavior¶
The time required to run the command depends on the total size of the database. Because the command must touch all data files, the command may take several seconds to run.
Accuracy after Unexpected Shutdown¶
After an unclean shutdown of a mongod using the Wired Tiger storage engine, count and size statistics reported by
dbStats may be inaccurate.
The amount of drift depends on the number of insert, update, or delete
operations performed between the last checkpoint and the unclean shutdown. Checkpoints
usually occur every 60 seconds. However, mongod instances running
with non-default --syncdelay settings may have more or less frequent
checkpoints.
Run validate on each collection on the mongod
to restore the correct statistics after an unclean shutdown.
Replica Set Member State Restriction¶
Starting in MongoDB 4.4, to run on a replica set member,
dbStats operations require the member to be in
PRIMARY or SECONDARY state. If the member
is in another state, such as STARTUP2, the
operation errors.
In previous versions, the operations can also be run when the member
is in STARTUP2. However, the operations wait
until the member transitions to RECOVERING.
Output¶
- 
dbStats.db¶
- Contains the name of the database. 
- 
dbStats.collections¶
- Contains a count of the number of collections in that database. 
- 
dbStats.objects¶
- Contains a count of the number of objects (i.e. documents) in the database across all collections. 
- 
dbStats.avgObjSize¶
- The average size of each document in bytes. This is the - dataSizedivided by the number of documents. The scale argument does not affect the- avgObjSizevalue.
- 
dbStats.dataSize¶
- The total size of the uncompressed data held in this database. The - dataSizedecreases when you remove documents.- For databases using the WiredTiger storage engine, - dataSizemay be larger than- storageSizeif compression is enabled. The- dataSizedecreases when documents shrink.
- 
dbStats.storageSize¶
- The total amount of space allocated to collections in this database for document storage. The - storageSizedoes not decrease as you remove or shrink documents. This value may be smaller than- dataSizefor databases using the WiredTiger storage engine with compression enabled.- storageSizedoes not include the index size. See- indexSizefor index sizing.
- 
dbStats.indexes¶
- Contains a count of the total number of indexes across all collections in the database. 
- 
dbStats.indexSize¶
- The total size of all indexes created on this database. 
- 
dbStats.totalSize¶
- The sum of the - storageSizeand- indexSize.- New in version 4.4. 
- 
dbStats.scaleFactor¶
- The - scalevalue used by the command.- If you specified a non-integer scale factor, MongoDB uses the integer part of the specified factor. For example, if you specify a scale factor of - 1023.999, MongoDB uses- 1023as the scale factor.- New in version 4.2. 
- 
dbStats.fsUsedSize¶
- New in version 3.6. - Total size of all disk space in use on the filesystem where MongoDB stores data. - See also 
- 
dbStats.fsTotalSize¶
- New in version 3.6. - Total size of all disk capacity on the filesystem where MongoDB stores data.