$collStats (aggregation)

On this page本页内容

Definition定义

$collStats

New in version 3.4.版本3.4中的新功能。

Returns statistics regarding a collection or view.返回有关集合或视图的统计信息。

The $collStats stage has the following prototype form:$collStats阶段具有以下原型形式:

{
  $collStats:
    {
      latencyStats: { histograms: <boolean> },
      storageStats: { scale: <number> },
      count: {},
      queryExecStats: {}
    }
}

The $collStats stage accepts an argument document with the following optional fields:$collStats阶段接受带有以下可选字段的参数文档:

Field NameDescription描述
latencyStats Adds latency statistics to the return document.延迟统计信息添加到返回文档中。
latencyStats.histograms Adds latency histogram information to the embedded documents in latencyStats if true.如果为true,则向latencyStats中的嵌入文档添加延迟直方图信息。
storageStats

Adds storage statistics to the return document.存储统计信息添加到返回文档中。

  • Specify an empty document (i.e. storageStats: {}) to use the default scale factor of 1 for the various size data. 指定一个空文档(即storageStats:{}),对各种大小的数据使用默认的比例因子1。Scale factor of 1 displays the returned sizes in bytes.比例因子1以字节为单位显示返回的大小。
  • Specify the scale factor (i.e. storageStats: { scale: <number> }) to use the specified scale factor for the various size data. 指定比例因子(即storageStats: { scale: <number> }),以便对各种大小的数据使用指定的比例因子。For example, to display kilobytes rather than bytes, specify a scale value of 1024.例如,要显示千字节而不是字节,请指定1024的刻度值。

    If you specify a non-integer scale factor, MongoDB uses the integer part of the specified factor. 如果指定非整数比例因子,MongoDB将使用指定因子的整数部分。For example, if you specify a scale factor of 1023.999, MongoDB uses 1023 as the scale factor.例如,如果指定比例因子1023.999,MongoDB将使用1023作为比例因子。

    The scale factor does not affect those sizes that specify the unit of measurement in the field name, such as "bytes currently in the cache".比例因子不影响在字段名中指定度量单位的大小,例如"bytes currently in the cache"

count

Adds the total number of documents in the collection to the return document.将集合中的文档总数添加到退货文档中。

Note

The count is based on the collection’s metadata, which provides a fast but sometimes inaccurate count for sharded clusters.计数基于集合的元数据,这为分片集群提供了快速但有时不准确的计数。

See count Field请参阅计数字段

New in version 3.6.版本3.6中的新功能。

queryExecStats

Adds query execution statistics to the return document.向返回文档添加查询执行统计信息

New in version 4.4.版本4.4中的新功能。

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 NameDescription描述
ns The namespace of the requested collection or view.请求的集合或视图的命名空间
shard

The name of the shard the output document corresponds to.输出文档对应的碎片的名称。

Only present when $collStats runs on a sharded cluster. 仅当$collStats在分片集群上运行时出现。Both sharded and non-sharded collections will produce this field.分片集合和非分片集合都将产生此字段。

New in version 3.6.版本3.6中的新功能。

host

The hostname and port of the mongod process which produced the output document.生成输出文档的mongod进程的主机名和端口。

New in version 3.6.版本3.6中的新功能。

localTime The current time on the MongoDB server, expressed as UTC milliseconds since the Unix epoch.MongoDB服务器上的当前时间,表示为自Unix纪元以来的UTC毫秒。
latencyStats

Statistics related to request latency for a collection or view. 与集合或视图的请求延迟相关的统计信息。See latencyStats Document for details on this document.有关此文档的详细信息,请参阅最新的系统测试文档

Only present when the latencyStats: {} option is specified.仅在指定latencyStats:{}选项时出现。

storageStats

Statistics related to a collection’s storage engine. 与集合的存储引擎相关的统计信息。See storageStats Document for details on this document.有关此文档的详细信息,请参阅storageStats文档

The various size data is scaled by the specified factor (with the exception of those sizes that specify the unit of measurement in the field name).各种大小的数据按指定的因子进行缩放(在字段名中指定测量单位的大小除外)。

Only present when the storageStats option is specified.仅在指定storageStats选项时出现。

Returns an error if applied to a view.如果应用于视图,则返回错误。

count

The total number of documents in the collection. 集合中的文档总数。This data is also available in storageStats.count.storageStats.count中也提供了这些数据。

Note

The count is based on the collection’s metadata, which provides a fast but sometimes inaccurate count for sharded clusters.计数基于集合的元数据,这为分片集群提供了快速但有时不准确的计数。

Only present when the count: {} option is specified. 仅在指定了count:{}选项时出现。Returns an error if applied to a view.如果应用于视图,则返回错误

queryExecStats

Statistics related to query execution for the collection.与集合的查询执行相关的统计信息。

Only present when the queryExecStats: {} option is specified. 仅在指定queryExecStats:{}选项时出现。Returns an error if applied to a view.如果应用于视图,则返回错误。

Behavior行为

$collStats must be the first stage in an aggregation pipeline, or else the pipeline returns an error.$collStats必须是聚合管道中的第一个阶段,否则管道将返回错误。

Transactions事务

$collStats is not allowed in transactions.事务中不允许使用$collStats

latencyStats Document文档

The latencyStats embedded document only exists in the output if you specify the latencyStats option.如果指定latencyStats选项,则latencyStats嵌入文档仅存在于输出中。

Field Name字段名Description描述
reads Latency statistics for read requests.读取请求的延迟统计信息。
writes Latency statistics for write requests.写入请求的延迟统计信息。
commands Latency statistics for database commands.数据库命令的延迟统计信息。

Each of these fields contains an embedded document bearing the following fields:每个字段都包含一个嵌入的文档,其中包含以下字段:

Field NameDescription描述
latency A 64-bit integer giving the total combined latency in microseconds.一个64位整数,给出以微秒为单位的总组合延迟。
ops A 64-bit integer giving the total number of operations performed on the collection since startup.一个64位整数,给出自启动以来对集合执行的操作总数。
histogram

An array of embedded documents, each representing a latency range. 一组嵌入式文档,每个文档代表一个延迟范围。Each document covers twice the previous document’s range. 每个文档覆盖的范围是前一个文档的两倍。For upper values between 2048 microseconds and roughly 1 second, the histogram includes half-steps.对于2048微秒到大约1秒之间的上限值,直方图包括半步。

This field only exists given the latencyStats: { histograms: true } option. 此字段仅在使用latencyStats: { histograms: true }选项时存在。Empty ranges with a zero count are omitted from the output.从输出中忽略count为零的空范围。

Each document bears the following fields:每个文档都包含以下字段:

Field Name字段名 Description描述
micros

A 64-bit integer giving the inclusive upper time bound of the current latency range in microseconds.一个64位整数,给出当前延迟范围的包含上限(以微秒为单位)。

The document’s range spans between the previous document’s micros value, exclusive, and this document’s micros value, inclusive.文档的范围介于上一个文档的micros值(独占)和此文档的micros值(包含)之间。

count A 64-bit integer giving the number of operations with latency less than or equal to micros.一个64位整数,表示延迟小于或等于micros的操作数。

For example, if collStats returns the following histogram:例如,如果collStats返回以下直方图:

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

This indicates that there were:这表明:

  • 10 operations taking 1 microsecond or less,10次操作需要1微秒或更短时间,
  • 1 operation in the range (1, 2] microseconds,在(1, 2]微秒范围内进行1次操作,
  • 1 operation in the range (3072, 4096] microseconds,在(3072, 4096]微秒范围内进行1次操作,
  • 1000 operations in the range (12288, 16384], and在(12288, 16384]微秒范围内进行1000次操作,以及
  • 100 operations in the range (32768, 49152].在(32768, 49152]微秒范围内进行100次操作。

For example, if you run $collStats with the latencyStats: {} option on a matrices collection:例如,如果在矩阵集合上使用latencyStats:{}选项运行$collStats

db.matrices.aggregate( [ { $collStats: { latencyStats: { histograms: true } } } ] )

This query returns a result similar to the following:此查询返回与以下类似的结果:

{ "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)
      }
    }
}

storageStats Document文档

The 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 $collStats with the storageStats: {} option on a matrices collection using the WiredTiger Storage Engine:例如,如果使用WiredTiger存储引擎对矩阵集合运行带有storageStats:{}选项的$collStats

db.matrices.aggregate( [ { $collStats: { storageStats: { } } } ] )

This query returns a result similar to the following:此查询返回与以下类似的结果:

{
  "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,  // Starting in MongoDB 4.4
    "capped" : false,
    "wiredTiger" : {
      ...
    },
    "nindexes" : 2,
    "indexDetails" : {
      ...
    },
    "indexBuilds" : [    // Starting in MongoDB 4.2
       "_id_1_abc_1"
    ],
    "totalIndexSize" : 260337664,
    "totalSize" : 613216256,    // Starting in MongoDB 4.4
    "indexSizes" : {
      "_id_" : 9891840,
      "_id_1_abc_1" : 250445824
    },
    "scaleFactor" : 1    // Starting in MongoDB 4.2
  }
}

See Output for a reference on this document.有关本文档的参考信息,请参阅输出

In-progress Indexes在建索引

Starting in MongoDB 4.2, the returned storageStats includes information on indexes being built. 从MongoDB 4.2开始,返回的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

db.matrices.aggregate( [ { $collStats: { count: { } } } ] )

The query returns a result similar to the following:查询将返回与以下类似的结果:

{
  "ns" : "test.matrices",
  "host" : mongo.example.net:27017",
  "localTime" : ISODate("2017-10-06T19:43:56.599Z"),
  "count" : 1103869
}

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文档

queryExecStats Document文档

New 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 NameDescription描述
total A 64-bit integer giving the total number of queries that performed a collection scan. 64位整数,表示执行集合扫描的查询总数。The total consists of queries that did and did not use a tailable cursor.总数由使用和未使用可裁剪游标的查询组成。
nonTailable A 64-bit integer giving the number of queries that performed a collection scan that did not use a tailable cursor.一个64位整数,给出执行不使用可裁剪游标的集合扫描的查询数。

For example, if you run $collStats with the queryExecStats: {} option on a matrices collection:例如,如果在矩阵集合上使用queryExecStats:{}选项运行$collStats

db.matrices.aggregate( [ { $collStats: { queryExecStats: { } } } ] )

The query returns a result similar to the following:查询将返回与以下类似的结果:

{
  "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 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

db.matrices.aggregate( [ { $collStats: { count: { } } } ] )

The query returns a result similar to the following:查询将返回与以下类似的结果:

{
  "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
}