On this page本页内容
collStats
¶The collStats
command returns a variety of storage statistics for a given collection.
To run collStats
, use the db.runCommand( { <command> } )
method.
The collStats
command has the following syntax:语法如下所示:
The command takes the following fields:
collStats |
string | The name of the target collection. If the collection does not exist, |
scale |
int |
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
.
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.
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 the correct statistics after an unclean shutdown.
Starting in MongoDB 4.2, the collStats
includes information on indexes currently being built. For details, see:
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
.
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.
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 size
does not include the size of any indexes associated with the collection, which the totalIndexSize
field reports.
The scale
argument affects this value. Data compression does not affect this value.
collStats.
count
¶The number of objects or documents in this collection.
collStats.
avgObjSize
¶The average size of an object in the collection. The scale
argument does not affect this value.
collStats.
storageSize
¶The total amount of storage allocated to this collection for document storage. The scale
argument 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 for collStats.size
.
storageSize
does not include index size. See totalIndexSize
for index sizing.
collStats.
freeStorageSize
¶Unavailable for the In-Memory Storage Engine
The amount of storage available for reuse. The scale
argument affects this value.
The field is only available if storage is available for reuse (i.e. greater than zero).
New in version 4.4.版本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, nindexes
includes 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, indexDetails
includes 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.版本4.2中的新功能。
collStats.
totalIndexSize
¶The total size of all indexes. The scale
argument 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, totalIndexSize
includes in its total the size of those indexes currently being built.
collStats.
totalSize
¶The sum of the storageSize
and totalIndexSize
. The scale
argument affects this value.
New in version 4.4.版本4.4中的新功能。
collStats.
indexSizes
¶This field specifies the key and size of every existing index on the collection. The scale
argument 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, indexSizes
includes sizes of indexes currently being built.
collStats.
scaleFactor
¶The scale
value 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 uses 1023
as the scale factor.
New in version 4.2.版本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
¶wiredTiger
only appears when using the WiredTiger storage engine.
This document contains data reported directly by the WiredTiger engine and other data for internal diagnostic use.