对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$collStats(聚合阶段)

$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"。

count

将集合中的文档总数添加到返回文档中。

该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。

请参阅 count 字段

queryExecStats

将查询执行统计数据添加到返回文档中。

对于副本集中的集合或集群中的非分片集合,$collStats 会输出单个文档。对于分片集合,$collStats 会为每个分片输出一份文档。输出文件包括以下字段:

字段名称
说明

ns

请求的集合或视图的命名空间。

shard

输出文档对应的分片的名称。

仅当 $collStats 在分片集群上运行时才出现。分片和非分片集合都会产生这个字段。

host

生成输出文档的 mongod 进程的主机名和端口。

localTime

MongoDB 服务器上的当前时间,表示为自 UNIX 纪元以来的 UTC 毫秒数。

latencyStats

与集合或视图的请求延迟有关的统计信息。有关此文档的详细信息,请参阅 latencyStats 文档。

仅当指定了 latencyStats: {} 选项时显示。

storageStats

与集合的存储引擎相关的统计数据。有关此文档的详细信息,请参阅 storageStats 文档。

各种尺寸数据按指定的比例因子缩放(在字段名称中指定测量单位的尺寸除外)。

仅当指定了 storageStats 选项时显示。

如果应用于视图,则返回错误。

count

集合中的文档总数。此数据也可在 storageStats.count 中找到。

该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。

仅当指定了 count: {} 选项时显示。如果应用于视图,则返回错误。

queryExecStats

与集合的查询执行相关的统计数据。

仅当指定了 queryExecStats: {} 选项时显示。如果应用于视图,则返回错误。

$collStats 必须是聚合管道中的第一阶段,否则管道将返回错误。

使用 Wiredmongod Tiger存储引擎的 非正常关闭后,$collStats 报告的大小和计数统计信息可能不准确。

偏差的大小取决于在最后一个检查点和非正常关闭之间执行的插入、更新或删除操作的次数。检查点通常每 60 秒出现一次。但是,如果 mongod 实例使用了非默认的 --syncdelay 设置,则检查点出现的次数可能会增多或减少。

在 mongod 的每个集合上运行 validate,以在非正常关闭之后恢复统计信息。

非正常关闭后:

  • validate使用最新值更新 collStats输出中的计数统计数据。

  • 其他统计信息(例如在 collStats 输出中插入或删除的文档数量)均为估计值。

使用 Queryable Encryption 时,$collStats 输出会编辑加密集合的某些信息:

  • 输出省略了 "queryExecStats"

  • 输出省略了 "latencyStats"

  • 如果显示,输出将编辑 "WiredTiger" 以只包含 url 字段。

$collStats 不允许在事务中使用。

如果指定 latencyStats 选项,则 latencyStats 嵌入文档仅存在于输出中。

字段名称
说明

reads

读取请求的延迟统计信息。

writes

写入请求的延迟统计数据。

commands

数据库命令的延迟统计信息。

transactions

数据库事务的延迟统计信息。

其中每个字段都包含一个包含以下字段的嵌入文档:

字段名称
说明

latency

一个 64 位整数,以微秒为单位表示总组合延迟。

ops

一个 64 位整数,表示自启动以来对集合执行的操作总数。

histogram

嵌入式文档大量,每个文档代表一个延迟范围。 每个文档的覆盖范围是前一个文档范围的两倍。 对于介于2048微秒到大约1秒之间的较低值,直方图包括半步。

此字段仅在给定 latencyStats: { histograms: true } 选项时存在。输出中将省略带有零 count 的空范围。

每个文档都包含以下字段:

字段名称

说明

micros

一个64位整数,给出当前延迟范围的包含时间下限(以微秒为单位)。

文档的范围介于上一文档的 micros 值(不含)和本文档的 micros 值(含)之间。

count

一个 64 位整数,表示延迟小于或等于 micros 的操作次数。

例如,如果 collStats 返回以下直方图:

histogram: [ { micros: Long(0), count: Long(10) }, { micros: Long(2), count: Long(1) }, { micros: Long(4096), count: Long(1) }, { micros: Long(16384), count: Long(1000) }, { micros: Long(49152), count: Long(100) }]

This indicates that there were [1]:

  • 10操作用时2微秒或更短

  • 1操作,范围[ 2 , 4 ) 微秒

  • 1操作,范围[ 4096 , 6144 ) 微秒

  • 在 [ 16384 , 24576 ) 微秒范围执行1000次操作

  • 在 [ 49152 , 65536 ) 微秒范围执行100次操作

[1]
  • 此页上的 ( 符号表示表示该值不包含在内。

  • 本页中的 ] 符号表示该值包含在内。

某些高延迟的$lookup 操作可能不会为外部集合生成慢速查询日志。发生这种情况的原因是,慢速查询日志与数据库分析器中报告的操作相对应,而延迟指标仅在获取集合锁时才会递增。

如果分片上的 $lookup查询可以执行本地读取,则 $lookup 不会记录用于查询外部集合的单独操作。 本地读取是指对外部集合的查询仅针对执行当前操作的同一分片。 因此,$lookup 操作会增加 $collStats延迟指标和操作计数,但不会为外部集合生成慢速查询日志。

如果指定 storageStats 选项,则 storageStats 嵌入文档仅存在于输出中。

本文档的内容取决于所使用的存储引擎。有关本文档的参考,请参阅输出。

当您使用 storageStats: {} 选项对时间序列集合运行$collStats 时,输出包括时间序列数据。

要学习;了解有关 timeseries: {}文档中返回字段的更多信息,请参阅 bucketCatalog。

在视图上使用 storageStats 选项执行 $collStats 会导致错误。

如果指定 count 选项,则 count 字段仅存在于输出中。

注意

该计数基于集合的元数据,它为分片集群提供快速但有时不准确的计数。

当指定 storageStats: {} 时,集合中的文档总数也可作为 storageStats.count 获得。有关更多信息,请参阅storageStats 文档。

如果指定 queryExecStats 选项,则 queryExecStats嵌入式文档仅存在于输出中。它包括一个嵌入式 collectionScans文档,其中包含以下字段:

字段名称
说明

total

执行集合扫描的查询总数。

nonTailable

执行集合扫描但未使用可追加游标的查询数量。

本页上的示例使用来自 sample_mflix 数据集的数据。有关如何将此数据集加载到您的自管理 MongoDB 部署中的详细信息,请参阅 加载示例数据集。如果您对示例数据库进行了任何修改,您可能需要删除并重新创建数据库,才能运行此页面上的示例。

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: '...' } } }]

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 } }]

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 }]

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 在分片集合上运行时,将为每个分片输出一个文档。每个输出文档都包含一个 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}
给本页内容打分

文档