- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.stats()
db.collection.stats()¶
On this page
Definition¶
-
db.collection.stats(<option>)¶ mongoShell MethodThis page documents the
mongoshell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.Returns statistics about the collection.
The method has the following format:
Field Type Description scalenumber 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.indexDetailsboolean Optional. If
true,db.collection.stats()returnsindex detailsin addition to the collection stats.Only works for WiredTiger storage engine.
Defaults to
false.indexDetailsKeydocument Optional. If
indexDetailsistrue, you can useindexDetailsKeyto filter index details by specifying the index key specification. Only the index that exactly matchesindexDetailsKeywill be returned.If no match is found,
indexDetailswill display statistics for all indexes.Use
getIndexes()to discover index keys. You cannot useindexDetailsKeywithindexDetailsName.indexDetailsNamestring Optional. If
indexDetailsistrue, you can useindexDetailsNameto filter index details by specifying the indexname. Only the index name that exactly matchesindexDetailsNamewill be returned.If no match is found,
indexDetailswill display statistics for all indexes.Use
getIndexes()to discover index names. You cannot useindexDetailsNamewithindexDetailsField.To specify just the
scalefactor, MongoDB supports the legacy format:Returns: A document that contains statistics on the specified collection. See collStatsfor a breakdown of the returned statistics.
The db.collection.stats() method provides a wrapper
around the database command collStats.
Behavior¶
Scaled Sizes¶
Unless otherwise specified by the metric name (such as "bytes
currently in the cache"), values related to size are displayed in
bytes and can be overridden by scale.
The scale factor rounds the affected size values to whole numbers.
Storage Engine¶
Depending on the storage engine, the data returned may differ. For details on the fields, see output details.
Accuracy after Unexpected Shutdown¶
After an unclean shutdown of a mongod using the Wired Tiger storage engine, count and size statistics reported by
db.collection.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.
Replica Set Member State Restriction¶
Starting in MongoDB 4.4, to run on a replica set member,
collStats 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.
Index Filter Behavior¶
Filtering on indexDetails using either indexDetailsKey or
indexDetailsName will only return a single matching index.
If no exact match is found, indexDetails will show information
on all indexes for the collection.
The indexDetailsKey field takes a document of the following form:
Where <string>> is the field that is indexed and <value> is either
the direction of the index, or the special index type such as text or
2dsphere. See index types for the full list of
index types.
Unexpected Shutdown and Count¶
For MongoDB instances using the WiredTiger
storage engine, after an unclean shutdown, statistics on size and count
may off by up to 1000 documents as reported by collStats,
dbStats, count. To restore the correct
statistics for the collection, run validate on the
collection.
In-Progress Indexes¶
Starting in MongoDB 4.2, the db.collection.stats includes
information on indexes currently being built. For details, see:
Examples¶
Note
You can find the collection data used for these examples in our primer-dataset.json
Basic Stats Lookup¶
The following operation returns stats on the restaurants collection
in the test database:
The operation returns:
As stats was not give a scale parameter, all size values are in bytes.
Stats Lookup With Scale¶
The following operation changes the scale of data from bytes to kilobytes
by specifying a scale of 1024:
The operation returns:
Statistics Lookup With Index Details¶
The following operation creates an indexDetails document that contains
information related to each of the indexes within the collection:
The operation returns:
Statistics Lookup With Filtered Index Details¶
To filter the indexes in the indexDetails field, you can
either specify the index keys using the indexDetailsKey option or specify
the index name using the indexDetailsName. To discover index keys and
names for the collection, use db.collection.getIndexes().
Given the following index:
The following operation filters the indexDetails document to a single
index as defined by the indexDetailsKey document.
The following operation filters the indexDetails document to a single
index as defined by the indexDetailsName document.
Both operations will return the same output:
For explanation of the output, see output details.
See also