- Reference >
- Database Commands >
- Diagnostic Commands >
- collStats
collStats¶
On this page
Definition¶
-
collStats¶ The
collStatscommand returns a variety of storage statistics for a given collection.To run
collStats, use thedb.runCommand( { <command> } )method.
Syntax¶
The command has the following syntax:
Command Fields¶
The command takes the following fields:
| Field | Type | Description |
|---|---|---|
collStats |
string | The name of the target collection. |
scale |
int | Optional. The scale factor for the various size data (with the exception of
those sizes that specify the unit of measurement in the field name). The
value defaults to 1 to return size data in bytes. To display
kilobytes rather than bytes, specify a 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 The scale factor rounds the affected size values to whole numbers. Starting in version 4.2, the output includes the |
Note
Starting in version 4.2, MongoDB removes the MMAPv1 storage
engine. In conjunction with this change, MongoDB removes the
MMAPv1 specific collStats option verbose.
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.
Accuracy after Unexpected Shutdown¶
After an unclean shutdown of a mongod using the Wired Tiger storage engine, size statistics reported by
collStats 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:
In-Progress Indexes¶
Starting in MongoDB 4.2, the collStats includes
information on indexes currently being built. For details, see:
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 also run when the member
is in STARTUP2. The operations wait until the member
transitioned to RECOVERING.
Example¶
The following operation runs the collStats command on the
restaurants collection, specifying a scale of 1024 bytes:
The following document provides a representation of the
collStats output. Depending on the configuration of your
collection and the storage engine, the output fields may vary.
Output¶
-
collStats.ns¶ The namespace of the current collection, which follows the format
[database].[collection].
-
collStats.size¶ The total uncompressed size in memory of all records in a collection. The
sizedoes not include the size of any indexes associated with the collection, which thetotalIndexSizefield reports.The
scaleargument affects this value. Data compression does not affect this value.
-
collStats.timeseries¶ timeseriesappears when you run thecollStatscommand on a time series collection.This document contains data for internal diagnostic use.
-
collStats.count¶ The number of objects or documents in this collection.
-
collStats.avgObjSize¶ The average size of an object in the collection. The
scaleargument does not affect this value.
-
collStats.numOrphanDocs¶ The number of orphaned documents in the collection.
New in version 6.0.
-
collStats.storageSize¶ The total amount of storage allocated to this collection for document storage. The
scaleargument affects this value.If collection data is compressed (which is the
default for WiredTiger), the storage size reflects the compressed size and may be smaller than the value forcollStats.size.storageSizedoes not include index size. SeetotalIndexSizefor index sizing.
-
collStats.freeStorageSize¶ Unavailable for the In-Memory Storage Engine
The amount of storage available for reuse. The
scaleargument affects this value.The field is only available if storage is available for reuse (i.e. greater than zero).
New in version 4.4.
-
collStats.nindexes¶ The number of indexes on the collection. All collections have at least one index on the _id field.
Starting in MongoDB 4.2,
nindexesincludes in its count those indexes currently being built.
-
collStats.indexDetails¶ A document that reports data from the WiredTiger storage engine for each index in the collection. Other storage engines will return an empty document.
The fields in this document are the names of the indexes, while the values themselves are documents that contain statistics for the index provided by the storage engine. These statistics are for internal diagnostic use.
Starting in MongoDB 4.2,
indexDetailsincludes details on indexes currently being built.
-
collStats.indexBuilds¶ An array that contains the names of the indexes that are currently being built on the collection. Once an index build completes, the index does not appear in the
indexBuilds.New in version 4.2.
-
collStats.totalIndexSize¶ The total size of all indexes. The
scaleargument affects this value.If an index uses prefix compression (which is the
default for WiredTiger), the returned size reflects the compressed size for any such indexes when calculating the total.Starting in MongoDB 4.2,
totalIndexSizeincludes in its total the size of those indexes currently being built.
-
collStats.totalSize¶ The sum of the
storageSizeandtotalIndexSize. Thescaleargument affects this value.New in version 4.4.
-
collStats.indexSizes¶ This field specifies the key and size of every existing index on the collection. The
scaleargument affects this value.If an index uses prefix compression (which is the
default for WiredTiger), the returned size reflects the compressed size.Starting in MongoDB 4.2,
indexSizesincludes sizes of indexes currently being built.
-
collStats.scaleFactor¶ The
scalevalue used by the command.If you had specified 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.New in version 4.2.
-
collStats.max¶ Shows the maximum number of documents that may be present in a capped collection.
-
collStats.maxSize¶ Shows the maximum size of a capped collection.
-
collStats.wiredTiger¶ wiredTigeronly appears when using the WiredTiger storage engine.This document contains data reported directly by the WiredTiger engine and other data for internal diagnostic use.