- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.stats()
db.collection.stats()¶
On this page
Definition¶
-
db.collection.stats(scale | options)¶ Returns statistics about the collection. The method includes the following parameters:
Parameter Type Description scalenumber Optional. The scale used in the output to display the sizes of items. By default, output displays sizes in
bytes. To display kilobytes rather than bytes, specify ascalevalue of1024.Changed in version 3.0: Legacy parameter format. Mutually exclusive with
optionsas a document.optionsdocument Optional. Alternative to
scaleparameter. Use theoptionsdocument to specify options, includingscale.New in version 3.0.
The
optionsdocument can contain the following fields and values:Field Type Description scalenumber Optional. The scale used in the output to display the sizes of items. By default, output displays sizes in bytes. To display kilobytes rather than bytes, specify a
scalevalue of1024.New in version 3.0.
indexDetailsboolean Optional. If
true,db.collection.stats()returnsindex detailsin addition to the collection stats.Only works for WiredTiger storage engine.
Defaults to
false.New in version 3.0.
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.New in version 3.0.
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.New in version 3.0.
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¶
This method returns a JSON document with statistics related to the current
mongod instance. Unless otherwise specified by the key name,
values related to size are displayed in bytes and can be overridden
by scale.
Note
The scale factor rounds values to whole numbers.
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.
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.
Examples¶
Note
You can find the collection data used for these examples in our Getting Started Guide
Basic Stats Lookup¶
The following operation returns stats on the restaurants collection:
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