定义
- $collStats
- 版本 3.4 中的新增功能。 - 返回有关集合或视图的统计信息。 - $collStats阶段具有以下原型形式:- { - $collStats: - { - latencyStats: { histograms: <boolean> }, - storageStats: { scale: <number> }, - count: {}, - queryExecStats: {} - } - } - $collStats阶段接受带有以下可选字段的参数文档:字段名称说明- latencyStats- latencyStats.histograms- 如果为 - true,将延迟直方图信息添加到- latencyStats中的嵌入文档。- storageStats- 指定一个空文档(即 - storageStats: {})对各种大小的数据使用默认比例因子 1。比例因子 1 显示返回的大小,以字节为单位。
- 指定比例因子(即 - storageStats: { scale: <number> })对各种大小数据使用指定的比例因子。例如,要显示千字节而不是字节,请指定比例值 1024。- 如果指定了非整数比例因子,MongoDB 将使用该因子的整数部分。例如,如果指定比例因子为 - 1023.999,MongoDB 将使用- 1023作为比例因子。- 比例因子不会影响在字段名称中指定测量单位的大小,例如 - "bytes currently in the cache"。
 - count- queryExecStats- 对于副本集中的集合或集群中的非分片集合, - $collStats会输出单个文档。对于分片集合,- $collStats会为每个分片输出一份文档。输出文件包括以下字段:字段名称说明- ns- 请求的集合或视图的命名空间。 - shard- host- 生成输出文档的 - mongod进程的主机名和端口。- 版本 3.6 中的新增功能。 - localTime- MongoDB 服务器上的当前时间,表示为自 UNIX 纪元以来的 UTC 毫秒数。 - latencyStats- 与集合或视图的请求延迟相关的统计信息。 - latencyStats有关此文档的详细信息,请参阅 文档。- 仅当指定了 - latencyStats: {}选项时显示。- storageStats- 与集合的存储引擎相关的统计信息。 - storageStats有关此文档的详细信息,请参阅 文档。- 各种尺寸数据按指定的比例因子缩放(在字段名称中指定测量单位的尺寸除外)。 - 仅当指定了 - storageStats选项时显示。- 如果应用于视图,则返回错误。 - count- 集合中的文档总数。此数据也可在 - storageStats.count中找到。- 该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。 - 仅当指定了 - count: {}选项时显示。如果应用于视图,则返回错误。- queryExecStats- 与集合的查询执行相关的统计数据。 - 仅当指定了 - queryExecStats: {}选项时显示。如果应用于视图,则返回错误。
行为
$collStats 必须是聚合管道中的第一阶段,否则管道将返回错误。
事务
$collStats 不允许在事务中使用。
latencyStats 文档
如果指定 latencyStats 选项,则 latencyStats 嵌入文档仅存在于输出中。
| 字段名称 | 说明 | 
|---|---|
| 
 | 读取请求的延迟统计信息。 | 
| 
 | 写入请求的延迟统计数据。 | 
| 
 | 数据库命令的延迟统计信息。 | 
其中每个字段都包含一个包含以下字段的嵌入文档:
| 字段名称 | 说明 | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 一个 64 位整数,以微秒为单位表示总组合延迟。 | |||||||||||||
| 
 | 一个 64 位整数,表示自启动以来对集合执行的操作总数。 | |||||||||||||
| 
 | 嵌入式文档大量,每个文档代表一个延迟范围。 每个文档的覆盖范围是前一个文档范围的两倍。 对于介于2048微秒到大约1秒之间的较低值,直方图包括半步。 此字段仅在给定  每个文档都包含以下字段: 
 例如,如果  这表明: 
 | 
| [1] | 
 | 
例如,在 matrices 集合上运行带有 latencyStats: {} 选项的 $collStats:
db.matrices.aggregate( [ { $collStats: { latencyStats: { histograms: true } } } ] ) 
此查询返回类似于以下内容的结果:
{ "ns" : "test.matrices",   "host" : "mongo.example.net:27017",   "localTime" : ISODate("2017-10-06T19:43:56.599Z"),   "latencyStats" :     { "reads" :         { "histogram" : [             { "micros" : NumberLong(16),               "count" : NumberLong(3) },             { "micros" : NumberLong(32),               "count" : NumberLong(1) },             { "micros" : NumberLong(128),               "count" : NumberLong(1) } ],           "latency" : NumberLong(264),           "ops" : NumberLong(5) },       "writes" :         { "histogram" : [             { "micros" : NumberLong(32),               "count" : NumberLong(1) },             { "micros" : NumberLong(64),               "count" : NumberLong(3) },             { "micros" : NumberLong(24576),               "count" : NumberLong(1) } ],           "latency" : NumberLong(27659),           "ops" : NumberLong(5) },       "commands" :         { "histogram" : [             {                "micros" : NumberLong(196608),                "count" : NumberLong(1)             }           ],           "latency" : NumberLong(0),           "ops" : NumberLong(0) },       "transactions" : {          "histogram" : [ ],          "latency" : NumberLong(0),          "ops" : NumberLong(0)       }     } } 
高延迟$lookup 操作
某些高延迟的$lookup 操作可能不会为外部集合生成慢速查询日志。发生这种情况的原因是,慢速查询日志与数据库分析器中报告的操作相对应,而延迟指标仅在获取集合锁时才会递增。
如果分片上的 $lookup查询可以执行本地读取,则 $lookup 不会记录用于查询外部集合的单独操作。 本地读取是指对外部集合的查询仅针对执行当前操作的同一分片。 因此,$lookup 操作会增加 $collStats延迟指标和操作计数,但不会为外部集合生成慢速查询日志。
storageStats 文档
如果指定 storageStats 选项,则 storageStats 嵌入文档仅存在于输出中。
本文档的内容取决于所使用的存储引擎。有关本文档的参考,请参阅输出。
例如,如果您使用 WiredTiger 存储引擎在 matrices 集合上运行带有 storageStats: {} 选项的 $collStats:
db.matrices.aggregate( [ { $collStats: { storageStats: { } } } ] ) 
此查询返回类似于以下内容的结果:
{   "ns" : "test.matrices",   "host" : "mongo.example.net:27017",   "localTime" : ISODate("2020-03-06T01:44:57.437Z"),   "storageStats" : {     "size" : 608500363,     "count" : 1104369,     "avgObjSize" : 550,     "storageSize" : 352878592,     "freeStorageSize" : 2490380,     "capped" : false,     "wiredTiger" : {       ...     },     "nindexes" : 2,     "indexDetails" : {       ...     },     "indexBuilds" : [    // Starting in MongoDB 4.2        "_id_1_abc_1"     ],     "totalIndexSize" : 260337664,     "totalSize" : 613216256,     "indexSizes" : {       "_id_" : 9891840,       "_id_1_abc_1" : 250445824     },     "scaleFactor" : 1    // Starting in MongoDB 4.2   } } 
有关本文档的参考,请参阅输出。
注意
进行中的索引
从 MongoDB 4.2 开始,返回的storageStats包含有关正在构建的索引的信息。 有关详细信息,请参阅:
在视图上使用storageStats选项执行$collStats会导致错误。
count 字段
版本 3.6 中的新增功能。
如果指定 count 选项,则 count 字段仅存在于输出中。
例如,在 matrices 集合上运行带有 count: {} 选项的 $collStats:
db.matrices.aggregate( [ { $collStats: { count: { } } } ] ) 
查询返回的结果如下:
{   "ns" : "test.matrices",   "host" : "mongo.example.net:27017",   "localTime" : ISODate("2017-10-06T19:43:56.599Z"),   "count" : 1103869 } 
注意
该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。
当指定 storageStats: {} 时,集合中的文档总数也可以作为 storageStats.count 可用。有关更多信息,请参阅 storageStats 文档。
queryExecStats 文档
如果指定 queryExecStats 选项,则 queryExecStats 嵌入文档仅存在于输出中。
collectionScans字段包含一个包含以下字段的嵌入式文档:
| 字段名称 | 说明 | 
|---|---|
| 
 | 一个 64 位整数,表示执行collection扫描的查询总数。总数包括使用和未使用可追加游标的查询。 | 
| 
 | 一个 64 位整数,给出执行未使用可追加游标的集合扫描的查询数量。 | 
例如,在 matrices 集合上运行带有 queryExecStats: {} 选项的 $collStats:
db.matrices.aggregate( [ { $collStats: { queryExecStats: { } } } ] ) 
查询返回的结果如下:
{   "ns": "test.matrices",   "host": "mongo.example.net:27017",   "localTime": ISODate("2020-06-03T14:23:29.711Z"),   "queryExecStats": {       "collectionScans": {           "total": NumberLong(33),           "nonTailable": NumberLong(31)       }   } } 
$collStats 在分片集合上
$collStats 在分片集合上运行时,将为每个分片输出一个文档。每个输出文档都包含一个 shard 字段,其中包含该文档对应的分片的名称。
例如,如果您在分片集合上运行 $collStats,并在名为 matrices 的集合上使用 count: {} 选项:
db.matrices.aggregate( [ { $collStats: { count: { } } } ] ) 
查询返回的结果如下:
{   "ns" : "test.matrices",   "shard" : "s1",   "host" : "s1-mongo1.example.net:27017",   "localTime" : ISODate("2017-10-06T15:14:21.258Z"),   "count" : 661705 } {   "ns" : "test.matrices",   "shard" : "s2",   "host" : "s2-mongo1.example.net:27017",   "localTime" : ISODate("2017-10-06T15:14:21.258Z"),   "count" : 442164 }