On this page本页内容
$collStats
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阶段接受带有以下可选字段的参数文档:
latencyStats | |
latencyStats.histograms | latencyStats if true.true,则将延迟直方图信息添加到latencyStats中的嵌入文档。
|
storageStats |
|
count |
|
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 | mongod process which produced the output document.mongod进程的主机名和端口。
|
localTime | |
latencyStats |
|
storageStats |
|
count |
|
queryExecStats |
|
$collStats must be the first stage in an aggregation pipeline, or else the pipeline returns an error.必须是聚合管道中的第一阶段,否则管道将返回错误。
事务中不允许使用$collStats is not allowed in transactions.$collStats。
latencyStatsThe 如果指定latencyStats embedded document only exists in the output if you specify the latencyStats option.latencyStats选项,则latencyStats嵌入文档仅存在于输出中。
| Field Name | |
|---|---|
reads | |
writes | |
commands |
Each of these fields contains an embedded document bearing the following fields:每个字段都包含一个嵌入文档,其中包含以下字段:
latency | |||||||||
ops | |||||||||
histogram |
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) }
]
|
For example, if you run 例如,如果在$collStats with the latencyStats: {} option on a matrices collection:matrices集合上使用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)
}
}
}
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:matrices集合上使用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.有关本文档的参考,请参阅输出。
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会导致错误。
countThe 如果指定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
}
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:matrices集合上使用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字段,其中包含文档对应的分片的名称。
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
}