db.stats()
On this page
Description
db.stats(scale)
Returns statistics that reflect the use state of a single database.
The
db.stats()
method is a wrapper around thedbStats
database command.
Parameters
The db.stats()
method has the following optional parameter:
Parameter | Type | Description |
---|---|---|
number | Optional. The scale factor for the various size data. The
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 Starting in version 4.2, the output includes the |
Output
The db.stats()
method returns a document with statistics reflecting
the database system's state. For example:
{ "db" : "test", "collections" : 6, "views" : 0, "objects" : 400629, "avgObjSize" : 663.996695196803, "dataSize" : 266016332, "storageSize" : 179838976, "indexes" : 13, "indexSize" : 9940992, "totalSize" : 189779968, "scaleFactor" : 1, "fsUsedSize" : 126727540736, "fsTotalSize" : 499963174912, "ok" : 1 }
For an explanation of the output, see Output.
Behavior
Accuracy after Unexpected Shutdown
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 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
.
Example
The following example returns various size values in kilobytes:
db.stats(1024)
Note
The scale factor rounds values to whole numbers.