Docs Menu

db.stats()

On this page

  • Description
  • Behavior
  • Example
db.stats(scale)

Returns statistics that reflect the use state of a single database.

The db.stats() method is a wrapper around the dbStats database command.

The db.stats() method has the following optional parameter:

Parameter
Type
Description
number

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.

The db.stats() method returns a document with statistics reflecting the database system's state. For example:

{
"db" : "admin",
"collections" : 2,
"views" : 0,
"objects" : 5,
"avgObjSize" : 336.8,
"dataSize" : 1684,
"storageSize" : 69632,
"freeStorageSize" : 28672,
"indexes" : 3,
"indexSize" : 106496,
"indexFreeStorageSize" : 45056,
"totalSize" : 176128,
"totalFreeStorageSize" : 73728,
"scaleFactor" : 1,
"fsUsedSize" : 47881682944,
"fsTotalSize" : 62725623808,
"ok" : 1
}

For an explanation of the output, see Output.

After an unclean shutdown of a mongod using the Wired Tiger storage engine, count and size statistics reported by db.stats() 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.

The following example returns various size values in kilobytes:

db.stats(1024)
Note

The scale factor rounds values to whole numbers.

You can also use db.stats() to return a single value, such as the freeStorageSize.

db.stats().freeStorageSize
db.stats(1024).freeStorageSize

The output shows the difference between the raw and scaled values.

28672
28
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

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