On this page本页内容
$collStats¶New in version 3.4.版本3.4中的新功能。
Returns statistics regarding a collection or view.返回有关集合或视图的统计信息。
The $collStats stage has the following prototype form:$collStats阶段具有以下原型形式:
The $collStats stage accepts an argument document with the following optional fields:$collStats阶段接受带有以下可选字段的参数文档:
| Field Name | |
|---|---|
latencyStats |
|
latencyStats.histograms |
latencyStats if true.true,则向latencyStats中的嵌入文档添加延迟直方图信息。 |
storageStats |
|
count |
Note
|
queryExecStats |
|
For a collection in a replica set or a non-sharded collection in a cluster, 对于副本集中的集合或集群中的 非分片集合,$collStats outputs a single document. $collStats输出单个文档。For a sharded collection, 对于分片集合,$collStats outputs one document per shard. $collStats会为每个分片输出一个文档。The output document includes the following fields:输出文档包括以下字段:
| Field Name | |
|---|---|
ns |
|
shard |
|
host |
|
localTime |
|
latencyStats |
|
storageStats |
|
count |
Note
|
queryExecStats |
|
$collStats must be the first stage in an aggregation pipeline, or else the pipeline returns an error.$collStats必须是聚合管道中的第一个阶段,否则管道将返回错误。
事务中不允许使用$collStats is not allowed in transactions.$collStats。
latencyStatsThe 如果指定latencyStats embedded document only exists in the output if you specify the latencyStats option.latencyStats选项,则latencyStats嵌入文档仅存在于输出中。
reads |
|
writes |
|
commands |
Each of these fields contains an embedded document bearing the following fields:每个字段都包含一个嵌入的文档,其中包含以下字段:
| Field Name | |||||||
|---|---|---|---|---|---|---|---|
latency |
|||||||
ops |
|||||||
histogram |
|
For example, if you run 例如,如果在矩阵集合上使用$collStats with the latencyStats: {} option on a matrices collection:latencyStats:{}选项运行$collStats:
This query returns a result similar to the following:此查询返回与以下类似的结果:
storageStatsThe 如果指定storageStats embedded document only exists in the output if you specify the storageStats option.storageStats选项,则storageStats嵌入文档仅存在于输出中。
The contents of this document are dependent on the storage engine in use. 本文档的内容取决于使用的存储引擎。See Output for a reference on this document.有关本文档的参考信息,请参阅输出。
For example, if you run 例如,如果使用WiredTiger存储引擎对矩阵集合运行带有$collStats with the storageStats: {} option on a matrices collection using the WiredTiger Storage Engine:storageStats:{}选项的$collStats:
This query returns a result similar to the following:此查询返回与以下类似的结果:
See Output for a reference on this document.有关本文档的参考信息,请参阅输出。
In-progress Indexes在建索引
Starting in MongoDB 4.2, the returned 从MongoDB 4.2开始,返回的storageStats includes information on indexes being built. storageStats包含有关正在构建的索引的信息。For details, see:有关详细信息,请参阅:
Performing 在视图上使用$collStats with the storageStats option on a view results in an error.storageStats选项执行$collStats会导致错误。
count Field¶New in version 3.6.版本3.6中的新功能。
The 只有在指定count field only exists in the output if you specify the count option.count选项时,count字段才存在于输出中。
For example, if you run 例如,如果在$collStats with the count: {} option on a matrices collection:matrices集合上使用count:{}选项运行$collStats:
The query returns a result similar to the following:查询将返回与以下类似的结果:
Note
The count is based on the collection’s metadata, which provides a fast but sometimes inaccurate count for sharded clusters.计数基于集合的元数据,这为分片集群提供了快速但有时不准确的计数。
The total number of documents in the collection is also available as 当指定storageStats.count when storageStats: {} is specified. storageStats:{}时,集合中的文档总数也可以作为storageStats.count获得。For more information, see storageStats Document.有关更多信息,请参阅storageStats文档。
queryExecStatsNew in version 4.4.版本4.4中的新功能。
The 如果指定queryExecStats embedded document only exists in the output if you specify the queryExecStats option.queryExecStats选项,queryExecStats嵌入文档仅存在于输出中。
The collectionScans field contains an embedded document bearing the following fields:collectionScans字段包含包含以下字段的嵌入式文档:
| Field Name | |
|---|---|
total |
|
nonTailable |
For example, if you run 例如,如果在矩阵集合上使用$collStats with the queryExecStats: {} option on a matrices collection:queryExecStats:{}选项运行$collStats:
The query returns a result similar to the following:查询将返回与以下类似的结果:
$collStats on Sharded Collections$collStats¶$collStats outputs one document per shard when run on sharded collections. $collStats在分片集合上运行时,每个分片输出一个文档。Each output document contains a 每个输出文档都包含一个shard field with the name of the shard the document corresponds to.shard字段,其中包含文档对应的shard的名称。
For example, if you run 例如,如果在名为$collStats on a sharded collection with the count: {} option on a collection named matrices:matrices的集合上使用count:{}选项在分片集合上运行$collStats:
The query returns a result similar to the following:查询将返回与以下类似的结果:
See also参阅