On this page本页内容
db.collection.
stats
(<option>)¶mongo
Shell Method
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
Returns statistics about the collection.返回有关集合的统计信息。
The method has the following format:该方法具有以下格式:
scale |
number |
|
indexDetails |
boolean |
|
indexDetailsKey |
document |
|
indexDetailsName |
string |
|
To specify just the 为了只指定scale
factor, MongoDB supports the legacy format:scale
因子,MongoDB支持传统格式:
collStats for a breakdown of the returned statistics.collStats 以获取返回的统计数据的细分。 |
The db.collection.stats()
method provides a wrapper around the database command collStats
.db.collection.stats()
方法为数据库命令collStats
提供了一个包装器。
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.比例因子将受影响的大小值舍入为整数。
Depending on the storage engine, the data returned may differ. 根据存储引擎的不同,返回的数据可能会有所不同。For details on the fields, see output details.有关字段的详细信息,请参阅输出详细信息。
After an unclean shutdown of a 在使用Wired Tiger存储引擎不干净地关闭mongod
using the Wired Tiger storage engine, count and size statistics reported by db.collection.stats()
may be inaccurate.mongod
后,db.collection.stats()
报告的计数和大小统计数据可能不准确。
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. 检查点通常每60秒出现一次。However, 但是,使用非默认的mongod
instances running with non-default --syncdelay
settings may have more or less frequent checkpoints.--syncdelay
设置运行的mongod
实例可能有更多或更少的检查点。
Run 在validate
on each collection on the mongod
to restore the correct statistics after an unclean shutdown.mongod
上的每个集合上运行validate
,以在不干净的关闭后恢复正确的统计数据。
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,collStats
operations require the member to be in PRIMARY
or SECONDARY
state. collStats
操作要求该成员处于PRIMARY
或SECONDARY
状态。If the member is in another state, such as 如果成员处于另一种状态,例如STARTUP2
, the operation errors.STARTUP2
,则操作会出错。
In previous versions, the operations can also be run when the member is in 在以前的版本中,当成员在STARTUP2
. STARTUP2
中时,也可以运行这些操作。However, the operations wait until the member transitions to 但是,这些操作将等待成员转换到RECOVERING
.RECOVERING
。
Filtering on 使用indexDetails
using either indexDetailsKey
or indexDetailsName
will only return a single matching index. indexDetails
Key或indexDetailsName
对indexDetails
进行筛选将只返回一个匹配的索引。If no exact match is found, 如果没有找到精确匹配,indexDetails
will show information on all indexes for the collection.indexDetails
将显示集合的所有索引的信息。
The indexDetailsKey
field takes a document of the following form:indexDetailsKey
字段采用以下格式的文档:
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
. <string>
>是索引的字段,<value>
是索引的方向,或是特殊的索引类型,如text
或2dsphere
。See index types for the full list of index types.有关索引类型的完整列表,请参阅索引类型。
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 对于使用WiredTiger存储引擎的MongoDB实例,在不干净地关闭后,collStats
, dbStats
, count
. collStats
、dbStats
、count
报告的大小和计数统计数据最多可能会减少1000个文档。To restore the correct statistics for the collection, run 要恢复集合的正确统计信息,请对集合运行validate
on the collection.validate
。
Starting in MongoDB 4.2, the 从MongoDB 4.2开始,db.collection.stats
includes information on indexes currently being built. db.collection.stats
包含当前正在构建的索引的信息。For details, see:有关详细信息,请参阅:
Note
You can find the collection data used for these examples in our primer-dataset.json您可以在primer-dataset.json中找到用于这些示例的收集数据
The following operation returns stats on the 以下操作返回restaurants
collection in the test
database:test
数据库中restaurants
集合的统计信息:
The operation returns:操作返回:
As stats was not give a scale parameter, all size values are in 由于stats没有给出scale参数,所以所有大小值都以bytes
.bytes
(字节)为单位。
The following operation changes the scale of data from 以下操作通过指定bytes
to kilobytes
by specifying a scale
of 1024
:1024
的scale
,将数据的刻度从bytes
更改为kilobytes
:
The operation returns:操作返回:
The following operation creates an 以下操作将创建一个indexDetails
document that contains information related to each of the indexes within the collection:indexDetails
文档,其中包含与集合中每个索引相关的信息:
The operation returns:操作返回:
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
. indexDetails
字段中的索引,可以使用indexDetailsKey
选项指定索引键,也可以使用indexDetailsName
指定索引名称。To discover index keys and names for the collection, use 要查找集合的索引键和名称,请使用db.collection.getIndexes()
.db.collection.getIndexes()
。
Given the following index:考虑到以下指标:
The following operation filters the 以下操作将indexDetails
document to a single index as defined by the indexDetailsKey
document.indexDetails
文档筛选为indexDetailsKey
文档定义的单个索引。
The following operation filters the 以下操作将indexDetails
document to a single index as defined by the indexDetailsName
document.indexDetails
文档筛选为indexDetailsName
文档定义的单个索引。
Both operations will return the same output:两个操作将返回相同的输出:
For explanation of the output, see output details.有关输出的说明,请参阅输出详细信息。
See also参阅