Definition
dbStatsThe
dbStatscommand returns storage statistics for a given database. The command has the following syntax:db.runCommand( { dbStats: 1, scale: <number> //Optional } ) The
dbStatscommand takes the following fields:ParameterDescriptiondbStats1
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 ascalevalue of1024.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 uses1023as the scale factor.Starting in version 4.2, the output includes the
scaleFactorused to scale the size values.In the
mongoshell, thedb.stats()function provides a wrapper arounddbStats.
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 statistics after an unclean shutdown.
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 also run when the member
is in STARTUP2. The operations wait until the member
transitioned to RECOVERING.
Output
dbStats.viewsContains a count of the number of views in that database.
dbStats.objectsContains a count of the number of objects (i.e. documents) in the database across all collections.
dbStats.avgObjSizeThe average size of each document in bytes. This is the
dataSizedivided by the number of documents. The scale argument does not affect theavgObjSizevalue.
dbStats.dataSizeThe 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 thanstorageSizeif compression is enabled. ThedataSizedecreases when documents shrink.
dbStats.storageSizeThe 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 thandataSizefor databases using the WiredTiger storage engine with compression enabled.storageSizedoes not include the index size. SeeindexSizefor index sizing.
dbStats.indexesContains a count of the total number of indexes across all collections in the database.
dbStats.totalSizeThe sum of the
storageSizeandindexSize.New in version 4.4.
dbStats.scaleFactorThe
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 uses1023as the scale factor.New in version 4.2.