Docs Menu

dbStats

On this page

  • Definition
  • Behavior
  • Output
  • Examples
dbStats

The dbStats command returns storage statistics for a given database. The command has the following syntax:

db.runCommand( {
dbStats: 1,
scale: <number> // Optional
} )

The dbStats command takes the following fields:

Parameter
Description
dbStats
1

Optional. The scale factor for the various size data. The scale defaults to 1 to return size data in bytes. To display kilobytes rather than bytes, specify a scale value 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 1023 as the scale factor.

Starting in version 4.2, the output includes the scaleFactor used to scale the size values.

In mongosh, the db.stats() function provides a wrapper around dbStats.

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.

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.

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.

dbStats.db

Name of the database.

dbStats.collections

Number of collections in the database.

dbStats.views

Number of views in the database.

dbStats.objects

Number of objects (specifically, documents) in the database across all collections.

dbStats.avgObjSize

Average size of each document in bytes. This is the dataSize divided by the number of documents. The scale argument does not affect the avgObjSize value.

dbStats.dataSize

Total size of the uncompressed data held in the database. The dataSize decreases when you remove documents.

For databases using the WiredTiger storage engine, dataSize may be larger than storageSize if compression is enabled. The dataSize decreases when documents shrink.

dbStats.storageSize

Sum of the space allocated to all collections in the database for document storage, including free space.

The storageSize does not decrease as you remove or shrink documents. This value may be smaller than dataSize for databases using the WiredTiger storage engine with compression enabled.

storageSize does not include space allocated to indexes. See indexSize for the total index size.

dbStats.freeStorageSize

Sum of the free space allocated to all collections in the database for document storage. Free database storage space is allocated to the collection but does not contain data.

freeStorageSize does not include free space allocated to indexes. See indexFreeStorageSize for the total free index size.

This value is always included in the dbStats output.

New in version 5.0.

dbStats.indexes

Total number of indexes across all collections in the database.

dbStats.indexSize

Sum of the space allocated to all indexes in the database, including free index space.

dbStats.indexFreeStorageSize

Sum of the free space allocated to all indexes in the database. Free database storage space is allocated to the index but does not contain data.

indexFreeStorageSize does not include free space allocated to document storage. See freeStorageSize for the total free document storage size.

New in version 5.0.

dbStats.totalSize

Sum of the space allocated for both documents and indexes in all collections in the database. Includes used and free storage space. This is the sum of storageSize and indexSize.

New in version 4.4.

dbStats.totalFreeStorageSize

Sum of the free storage space allocated for both documents and indexes in all collections in the database. This is the sum of freeStorageSize and indexFreeStorageSize.

New in version 5.0.

dbStats.scaleFactor

scale value 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 1023 as the scale factor.

New in version 4.2.

dbStats.fsUsedSize

Total size of all disk space in use on the filesystem where MongoDB stores data.

New in version 3.6.

Tip
See also:
dbStats.fsTotalSize

Total size of all disk capacity on the filesystem where MongoDB stores data.

If you are only interested in one field, the output of the dbStats command can be limited to just that field. For example, the following command returns the current total free storage size.

db.runCommand( { dbStats: 1 } ).freeStorageSize
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.