- Reference >
mongo
Shell 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 scale
number 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 ascale
value of1024
.Changed in version 3.0: Legacy parameter format. Mutually exclusive with
options
as a document.options
document Optional. Alternative to
scale
parameter. Use theoptions
document to specify options, includingscale
.New in version 3.0.
The
options
document can contain the following fields and values:Field Type Description scale
number 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
scale
value of1024
.New in version 3.0.
indexDetails
boolean Optional. If
true
,db.collection.stats()
returnsindex details
in addition to the collection stats.Only works for WiredTiger storage engine.
Defaults to
false
.New in version 3.0.
indexDetailsKey
document Optional. If
indexDetails
istrue
, you can useindexDetailsKey
to filter index details by specifying the index key specification. Only the index that exactly matchesindexDetailsKey
will be returned.If no match is found,
indexDetails
will display statistics for all indexes.Use
getIndexes()
to discover index keys. You cannot useindexDetailsKey
withindexDetailsName
.New in version 3.0.
indexDetailsName
string Optional. If
indexDetails
istrue
, you can useindexDetailsName
to filter index details by specifying the indexname
. Only the index name that exactly matchesindexDetailsName
will be returned.If no match is found,
indexDetails
will display statistics for all indexes.Use
getIndexes()
to discover index names. You cannot useindexDetailsName
withindexDetailsField
.New in version 3.0.
Returns: A document that contains statistics on the specified collection. See collStats
for 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.