$indexStats (aggregation)

在本页面

Definition

返回有关集合每个索引使用情况的统计信息。如果使用access control运行,则用户必须具有包括indexStats操作的特权。

$indexStats阶段采用空文档,语法如下:

{ $indexStats: { } }

return凭证包括以下字段:

Output Field Description
name Index name.
key 索引键规范。
host mongod进程的主机名和端口。
accesses 索引使用情况统计:


ops是使用索引的操作数。
since是 MongoDB 收集统计信息的时间。

索引的统计信息将在mongod重新启动或删除索引并重新创建时重置。

Note

在版本 3.2.3 之前,ops字段值不包括使用索引的$matchmapReduce操作。

Behavior

accesses字段报告的统计信息仅包括由用户请求驱动的索引访问。它不包括内部操作,例如通过TTL Indexes删除或块拆分和迁移操作。

$indexStats必须是聚合管道中的第一阶段。

Example

例如,集合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" }

在集合上创建以下两个索引:

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

对集合运行一些查询:

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

要查看有关orders集合使用索引的统计信息,请运行以下聚合操作:

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

该操作返回一个文档,其中包含每个索引的使用情况统计信息:

{
   "name" : "item_1_quantity_1",
   "key" : {
      "item" : 1,
      "quantity" : 1
   },
   "host" : "examplehost.local:27017",
   "accesses" : {
      "ops" : NumberLong(1),
      "since" : ISODate("2015-10-02T14:31:53.685Z")
   }
}
{
   "name" : "_id_",
   "key" : {
      "_id" : 1
   },
   "host" : "examplehost.local:27017",
   "accesses" : {
      "ops" : NumberLong(0),
      "since" : ISODate("2015-10-02T14:31:32.479Z")
   }
}
{
   "name" : "type_1_item_1",
   "key" : {
      "type" : 1,
      "item" : 1
   },
   "host" : "examplehost.local:27017",
   "accesses" : {
      "ops" : NumberLong(1),
      "since" : ISODate("2015-10-02T14:31:58.321Z")
   }
}
首页