定义
$collStats返回有关集合或视图的统计信息。
$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"。
countqueryExecStats将查询执行统计数据添加到返回文档中。
对于副本集中的集合或集群中的非分片集合,
$collStats会输出单个文档。对于分片集合,$collStats会为每个分片输出一份文档。输出文件包括以下字段:字段名称说明ns请求的集合或视图的命名空间。
shard输出文档对应的分片的名称。
仅当
$collStats在分片集群上运行时才出现。分片和非分片集合都会产生这个字段。host生成输出文档的
mongod进程的主机名和端口。localTimeMongoDB 服务器上的当前时间,表示为自 UNIX 纪元以来的 UTC 毫秒数。
latencyStats与集合或视图的请求延迟有关的统计信息。有关此文档的详细信息,请参阅 latencyStats 文档。
仅当指定了
latencyStats: {}选项时显示。storageStats与集合的存储引擎相关的统计数据。有关此文档的详细信息,请参阅 storageStats 文档。
各种尺寸数据按指定的比例因子缩放(在字段名称中指定测量单位的尺寸除外)。
仅当指定了
storageStats选项时显示。如果应用于视图,则返回错误。
count集合中的文档总数。此数据也可在
storageStats.count中找到。该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。
仅当指定了
count: {}选项时显示。如果应用于视图,则返回错误。queryExecStats与集合的查询执行相关的统计数据。
仅当指定了
queryExecStats: {}选项时显示。如果应用于视图,则返回错误。
行为
$collStats 必须是聚合管道中的第一阶段,否则管道将返回错误。
意外关闭后的准确性
使用 Wiredmongod Tiger存储引擎的 非正常关闭后,$collStats 报告的大小和计数统计信息可能不准确。
偏差的大小取决于在最后一个检查点和非正常关闭之间执行的插入、更新或删除操作的次数。检查点通常每 60 秒出现一次。但是,如果 mongod 实例使用了非默认的 --syncdelay 设置,则检查点出现的次数可能会增多或减少。
在 mongod 的每个集合上运行 validate,以在非正常关闭之后恢复统计信息。
非正常关闭后:
日志校订
使用 Queryable Encryption 时,$collStats 输出会编辑加密集合的某些信息:
输出省略了
"queryExecStats"输出省略了
"latencyStats"如果显示,输出将编辑
"WiredTiger"以只包含url字段。
事务
$collStats 不允许在事务中使用。
输出
latencyStats Document
如果指定 latencyStats 选项,则 latencyStats 嵌入文档仅存在于输出中。
字段名称 | 说明 |
|---|---|
| 读取请求的延迟统计信息。 |
| 写入请求的延迟统计数据。 |
| 数据库命令的延迟统计信息。 |
| 数据库事务的延迟统计信息。 |
其中每个字段都包含一个包含以下字段的嵌入文档:
字段名称 | 说明 | ||||||
|---|---|---|---|---|---|---|---|
| 一个 64 位整数,以微秒为单位表示总组合延迟。 | ||||||
| 一个 64 位整数,表示自启动以来对集合执行的操作总数。 | ||||||
| 嵌入式文档大量,每个文档代表一个延迟范围。 每个文档的覆盖范围是前一个文档范围的两倍。 对于介于2048微秒到大约1秒之间的较低值,直方图包括半步。 此字段仅在给定 每个文档都包含以下字段:
例如,如果 This indicates that there were [1]:
|
| [1] |
|
高延迟 $lookup 操作
某些高延迟的$lookup 操作可能不会为外部集合生成慢速查询日志。发生这种情况的原因是,慢速查询日志与数据库分析器中报告的操作相对应,而延迟指标仅在获取集合锁时才会递增。
如果分片上的 $lookup查询可以执行本地读取,则 $lookup 不会记录用于查询外部集合的单独操作。 本地读取是指对外部集合的查询仅针对执行当前操作的同一分片。 因此,$lookup 操作会增加 $collStats延迟指标和操作计数,但不会为外部集合生成慢速查询日志。
storageStats 文档
如果指定 storageStats 选项,则 storageStats 嵌入文档仅存在于输出中。
本文档的内容取决于所使用的存储引擎。有关本文档的参考,请参阅输出。
时间序列集合上的 storageStats 输出
当您使用 storageStats: {} 选项对时间序列集合运行$collStats 时,输出包括时间序列数据。
要学习;了解有关 timeseries: {}文档中返回字段的更多信息,请参阅 bucketCatalog。
在视图上使用 storageStats 选项执行 $collStats 会导致错误。
计数字段
如果指定 count 选项,则 count 字段仅存在于输出中。
注意
该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。
当指定 storageStats: {} 时,集合中的文档总数也可作为 storageStats.count 获得。有关更多信息,请参阅storageStats 文档。
queryExecStats Document
如果指定 queryExecStats 选项,则 queryExecStats嵌入式文档仅存在于输出中。它包括一个嵌入式 collectionScans文档,其中包含以下字段:
字段名称 | 说明 |
|---|---|
| 执行集合扫描的查询总数。 |
| 执行集合扫描但未使用可追加游标的查询数量。 |
示例
本页上的示例使用来自 sample_mflix 数据集的数据。有关如何将此数据集加载到您的自管理 MongoDB 部署中的详细信息,请参阅 加载示例数据集。如果您对示例数据库进行了任何修改,您可能需要删除并重新创建数据库,才能运行此页面上的示例。
latencyStats
This example runs $collStats with the latencyStats: {} option on the movies collection:
db.movies.aggregate( [ { $collStats: { latencyStats: { histograms: true } } } ] )[ { ns: 'sample_mflix.movies', host: '...', localTime: '...', latencyStats: { reads: { histogram: '...', latency: '...', ops: '...', queryableEncryptionLatencyMicros: '...' }, writes: { histogram: '...', latency: '...', ops: '...', queryableEncryptionLatencyMicros: '...' }, commands: { histogram: '...', latency: '...', ops: '...', queryableEncryptionLatencyMicros: '...' }, transactions: { histogram: '...', latency: '...', ops: '...', queryableEncryptionLatencyMicros: '...' } } }]storageStats
This example runs $collStats with the storageStats: {} option on the movies collection using the WiredTiger Storage Engine:
db.movies.aggregate( [ { $collStats: { storageStats: { } } } ] )[ { ns: 'sample_mflix.movies', host: '...', localTime: '...', storageStats: { size: '...', count: 21349, avgObjSize: '...', numOrphanDocs: 0, storageSize: '...', freeStorageSize: '...', capped: false, wiredTiger: '...', nindexes: '...', indexDetails: '...', indexBuilds: '...', totalIndexSize: '...', indexSizes: '...', totalSize: '...', scaleFactor: 1 } }]时间序列集合上的 storageStats
The weather collection is not included in the sample data. To create and populate the weather time series collection in the sample_mflix database, run the following code:
db.createCollection( "weather", { timeseries: { timeField: "timestamp", metaField: "sensorId" } } )db.weather.insertMany( [ { sensorId: 5578, timestamp: ISODate( "2024-01-01T00:00:00.000Z" ), temp: 12 }, { sensorId: 5578, timestamp: ISODate( "2024-01-01T06:00:00.000Z" ), temp: 14 }, { sensorId: 5578, timestamp: ISODate( "2024-01-01T12:00:00.000Z" ), temp: 16 }, { sensorId: 5579, timestamp: ISODate( "2024-01-01T00:00:00.000Z" ), temp: 9 }, { sensorId: 5579, timestamp: ISODate( "2024-01-01T06:00:00.000Z" ), temp: 11 }] )This example runs $collStats with the storageStats: {} option on the weather time series collection using the WiredTiger Storage Engine, and filters for only time series data:
db.weather.aggregate( [ { $collStats: { storageStats: { } } } ] ).toArray()[0].storageStats.timeseries{ bucketsNs: 'sample_mflix.system.buckets.weather', bucketCount: '...', avgBucketSize: '...', numActiveBuckets: '...', numBucketInserts: '...', numBucketUpdates: '...', numBucketsOpenedDueToMetadata: '...', numBucketsClosedDueToCount: '...', numBucketsClosedDueToSchemaChange: '...', numBucketsClosedDueToSize: '...', numBucketsClosedDueToTimeForward: '...', numBucketsClosedDueToMemoryThreshold: '...', numCommits: '...', numWaits: 0, numMeasurementsCommitted: '...', avgNumMeasurementsPerCommit: '...', numBucketsClosedDueToReopening: '...', numBucketsArchivedDueToMemoryThreshold: '...', numBucketsArchivedDueToTimeBackward: '...', numBucketsReopened: '...', numBucketsKeptOpenDueToLargeMeasurements: '...', numBucketsClosedDueToCachePressure: '...', numBucketsFrozen: '...', numCompressedBucketsConvertedToUnsorted: '...', numBucketsFetched: '...', numBucketsQueried: '...', numBucketFetchesFailed: '...', numBucketQueriesFailed: '...', numBucketReopeningsFailedDueToEraMismatch: '...', numBucketReopeningsFailedDueToMalformedIdField: '...', numBucketReopeningsFailedDueToHashCollision: '...', numBucketReopeningsFailedDueToMarkedFrozen: '...', numBucketReopeningsFailedDueToValidator: '...', numBucketReopeningsFailedDueToMarkedClosed: '...', numBucketReopeningsFailedDueToMinMaxCalculation: '...', numBucketReopeningsFailedDueToSchemaGeneration: '...', numBucketReopeningsFailedDueToUncompressedTimeColumn: '...', numBucketReopeningsFailedDueToCompressionFailure: '...', numBucketReopeningsFailedDueToWriteConflict: '...', numDuplicateBucketsReopened: '...', numBucketDocumentsTooLargeInsert: '...', numBucketDocumentsTooLargeUpdate: '...'}注意
进行中的索引
返回的 storageStats 包含有关正在构建的索引的信息。有关详细信息,请参阅:
数数
This example runs $collStats with the count: {} option on the movies collection:
db.movies.aggregate( [ { $collStats: { count: { } } } ] )[ { ns: 'sample_mflix.movies', host: '...', localTime: '...', count: 21349 }]queryExecStats
This example runs $collStats with the queryExecStats: {} option on the movies collection:
db.movies.aggregate( [ { $collStats: { queryExecStats: { } } } ] )[ { ns: 'sample_mflix.movies', host: '...', localTime: '...', queryExecStats: { collectionScans: { total: '...', nonTailable: '...' } } }]分片集合上的 $collStats
$collStats 在分片集合上运行时,将为每个分片输出一个文档。每个输出文档都包含一个 shard 字段,其中包含该文档对应的分片的名称。
例如,如果您在分片集合上运行 $collStats,并在名为 movies 的集合上使用 count: {} 选项:
db.movies.aggregate( [ { $collStats: { count: { } } } ] )查询返回的结果如下:
{ "ns" : "sample_mflix.movies", "shard" : "s1", "host" : "s1-mongo1.example.net:27017", "localTime" : ISODate("2017-10-06T15:14:21.258Z"), "count" : 661705}{ "ns" : "sample_mflix.movies", "shard" : "s2", "host" : "s2-mongo1.example.net:27017", "localTime" : ISODate("2017-10-06T15:14:21.258Z"), "count" : 442164}要使用MongoDB Node.js驱动程序将 $collStats 阶段添加到聚合管道,请在管道对象中使用 $collStats操作符。
以下示例演示了如何使用可用于 $collStats 阶段的选项。
latencyStats
以下示例使用latencyStats 选项创建并运行 $collStats管道阶段:
const pipeline = [ { $collStats: { latencyStats: {histograms: true} } }];
const cursor = collection.aggregate(pipeline); return cursor;storageStats
以下示例使用storageStats 选项创建并运行 $collStats管道阶段:
const pipeline = [{ $collStats: { storageStats: {} } }];
const cursor = collection.aggregate(pipeline);return cursor;时间序列集合上的 storageStats
以下示例在时间序列集合上使用storageStats 选项创建并运行 $collStats管道阶段,并仅筛选时间序列数据:
const pipeline = [{ $collStats: { storageStats: {} } }];
const cursor = collection.aggregate(pipeline);const timeSeriesStats = resultsTimeSeries[0].storageStats.timeseries;
return timeSeriesStats;数数
以下示例在集合上运行带有计数选项的 $collStats管道阶段:
const pipeline = [{ $collStats: { count: {} } }];
const cursor = collection.aggregate(pipeline);return cursor;queryExecStats
以下示例使用queryExecStats 选项创建并运行 $collStats管道阶段:
const pipeline = [{ $collStats: { queryExecStats: {} } }];
const cursor = collection.aggregate(pipeline);return cursor;