$indexStats (aggregation)

On this page本页内容

Definition定义

$indexStats

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

Returns statistics regarding the use of each index for the collection. 返回有关集合中每个索引使用情况的统计信息。If running with access control, the user must have privileges that include indexStats action.如果使用访问控制运行,用户必须具有包含indexStats操作的权限。

The $indexStats stage takes an empty document and has the following syntax:$indexStats阶段接受一个空文档,并具有如下语法:

{ $indexStats: { } }

For each index, the return document includes the following fields:对于每个索引,退货文档包括以下字段:

Output Field输出字段Description描述
name Index name.索引名。
key

Index key specification.索引键规范。

See also: spec.另请参见:规范

host

The hostname and port of the mongod process.mongod进程的主机名和端口。

accesses

Statistics on the index use:有关索引使用的统计信息:

  • ops is the number of operations that used the index.是使用索引的操作数。
  • since is the time from which MongoDB gathered the statistics.是MongoDB收集统计数据的时间。
shard

The name of the shard associated with the host主机关联的分片的名称

Only available for a sharded cluster.仅适用于分片群集。

New in version 4.2.4.在4.2.4版中新增。

spec

The full specfication document for the index, which includes the index key specification document.索引的完整规范文档,包括索引键规范文档。

The index option hidden, available starting in MongoDB 4.4, is only included if the value is true.从MongoDB 4.4开始提供的索引选项hidden仅在值为true时才包括在内。

New in version 4.2.4.在4.2.4版中新增。

building

Indicates if the index is currently being built.指示当前是否正在生成索引。

Only available if true.仅当为true时可用。

New in version 4.2.4.在4.2.4版中新增。

Statistics for an index will be reset on mongod restart or index drop and recreation.mongod重新启动或索引删除和重新创建时,索引的统计信息将被重置。

Note

Prior to version 3.2.3, the ops field value did not include $match or mapReduce operations that use indexes.在版本3.2.3之前,ops字段值不包括使用索引的$matchmapReduce操作。

Behavior行为

Accesses Field访问字段

The statistics reported by the accesses field only includes index access driven by user requests. accesses字段报告的统计信息仅包括由用户请求驱动的索引访问。It does not include internal operations like deletion via TTL Indexes or chunk split and migration operations.它不包括内部操作,如通过TTL索引删除或区块分割和迁移操作。

Restrictions限制

  • $indexStats must be the first stage in an aggregation pipeline.必须是聚合管道中的第一个阶段。
  • $indexStats is not allowed in transactions.事务中不允许使用$indexStats

Index Modifications Resets Statistics索引修改会重置统计信息

Modification of an existing index (see collMod command) resets the statistics for that index.修改现有索引(请参阅collMod命令)会重置该索引的统计信息。

Example示例

For example, a collection orders contains the following documents:例如,orders集合包含以下文档:

{ "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2, "type": "apparel" }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "type": "electronics" }
{ "_id" : 3, "item" : "abc", "price" : 10, "quantity" : 5, "type": "apparel" }

Create the following two indexes on the collection:在集合上创建以下两个索引:

db.orders.createIndex( { item: 1, quantity: 1 } )
db.orders.createIndex( { type: 1, item: 1 } )

Run some queries against the collection:对集合运行一些查询:

db.orders.find( { type: "apparel"} )
db.orders.find( { item: "abc" } ).sort( { quantity: 1 } )

To view statistics on the index use on the orders collection, run the following aggregation operation:要查看orders集合上索引使用的统计信息,请运行以下聚合操作:

db.orders.aggregate( [ { $indexStats: { } } ] )

The operation returns a document that contains usage statistics for each index:该操作返回一个文档,其中包含每个索引的使用统计信息:

{
   "name" : "item_1_quantity_1",
   "key" : { "item" : 1, "quantity" : 1 },
   "host" : "examplehost.local:27018",
   "accesses" : {
      "ops" : NumberLong(1),
      "since" : ISODate("2020-02-10T21:11:23.059Z")
   },
   "shard" : "shardA",      // Available starting in MongoDB 4.2.4 if run on sharded cluster
   "spec" : {               // Available starting in MongoDB 4.2.4
      "v" : 2,
      "key" : { "item" : 1, "quantity" : 1 },
      "name" : "item_1_quantity_1"
   }
}
{
   "name" : "item_1_price_1",
   "key" : { "item" : 1, "price" : 1 },
   "host" : "examplehost.local:27018",
   "accesses" : {
      "ops" : NumberLong(1),
      "since" : ISODate("2020-02-10T21:11:23.233Z")
   },
   "shard" : "shardA",      // Available starting in MongoDB 4.2.4 if run on sharded cluster
   "spec" : {               // Available starting in MongoDB 4.2.4
      "v" : 2,
      "key" : { "item" : 1, "price" : 1 },
      "name" : "item_1_price_1"
   }
}
{
   "name" : "item_1",
   "key" : { "item" : 1 },

   "host" : "examplehost.local:27018",
   "accesses" : {
      "ops" : NumberLong(0),
      "since" : ISODate("2020-02-10T21:11:22.947Z")
   },
   "shard" : "shardA",      // Available starting in MongoDB 4.2.4 if run on sharded cluster
   "spec" : {               // Available starting in MongoDB 4.2.4
      "v" : 2,
      "key" : { "item" : 1 },
      "name" : "item_1"
   }
}
{
   "name" : "_id_",
   "key" : { "_id" : 1 },
   "host" : "examplehost.local:27018",
   "accesses" : {
      "ops" : NumberLong(0),
      "since" : ISODate("2020-02-10T21:11:18.298Z")
   },
   "shard" : "shardA",      // Available starting in MongoDB 4.2.4 if run on sharded cluster
   "spec" : {               // Available starting in MongoDB 4.2.4
      "v" : 2,
      "key" : { "_id" : 1 },
      "name" : "_id_"
   }
}