Database Manual / Reference / Query Language / Aggregation Stages

$indexStats (aggregation stage)(聚合阶段)

Definition定义

$indexStats

Returns statistics regarding the use of each index for the collection. If running with access control, authenticate as a user with at least the clusterMonitor role.返回有关集合中每个索引使用情况的统计信息。如果使用访问控制运行,请以至少具有clusterMonitor角色的用户身份进行身份验证。

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描述
nameIndex 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.仅适用于分片集群。

spec

The full specification document for the index, which includes the index key and index properties.索引的完整规范文档,其中包括索引键和索引属性。

The index option hidden is only included if the value is true.只有当值为true时,才会包含hidden的索引选项。

building

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

Only available if true.只有为true时才可用。

Behavior行为

Accesses Field访问字段

The statistics reported by the accesses field only apply to the node where the query is being run and only include 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.事务中不允许。

Index Statistics Reset Considerations索引统计重置注意事项

  • Statistics for an index reset on mongod restart or index drop and recreation.mongod重启或索引删除和重新创建时的索引重置统计数据。
  • Modification of an existing index (see collMod command) resets the statistics for that index.修改现有索引(请参阅collMod命令)会重置该索引的统计信息。

Examples示例

MongoDB Shell

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

db.orders.insertMany( [
{ _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 } )

db.orders.createIndex(
{ price: 1 },
{ partialFilterExpression: { type: "apparel" } }
)

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

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

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: 'type_1_item_1',
key: { type: 1, item: 1 },
host: 'examplehost.local:27018',
accesses: { ops: Long("1"), since: ISODate("2024-05-02T15:07:21.420Z") },
shard: "shardA",
spec: { v: 2, key: { type: 1, item: 1 }, name: 'type_1_item_1' }
},
{
name: 'item_1_quantity_1',
key: { item: 1, quantity: 1 },
host: 'examplehost.local:27018',
accesses: { ops: Long("1"), since: ISODate("2024-05-02T15:07:21.254Z") },
shard: "shardA",
spec: { v: 2, key: { item: 1, quantity: 1 }, name: 'item_1_quantity_1' }
},
{
name: '_id_',
key: { _id: 1 },
host: 'examplehost.local:27018',
accesses: { ops: Long("0"), since: ISODate("2024-05-02T15:07:13.274Z") },
shard: "shardA",
spec: { v: 2, key: { _id: 1 }, name: '_id_' }
},
{
name: 'price_1',
key: { price: 1 },
host: 'examplehost.local:27018',
accesses: { ops: Long("0"), since: ISODate("2024-05-02T15:07:54.847Z") },
shard: "shardA",
spec: {
v: 2,
key: { price: 1 },
name: 'price_1',
partialFilterExpression: { type: 'apparel' }
}
}
]
Node.js

To use the MongoDB Node.js driver to add a $indexStats stage to an aggregation pipeline, use the $indexStats operator in a pipeline object.要使用MongoDB Node.js驱动程序将$indexStats阶段添加到聚合管道中,请在管道对象中使用$indexStats运算符。

The following example creates a pipeline stage that returns a document that contains usage statistics for each index on the collection. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,该阶段返回一个文档,其中包含集合上每个索引的使用统计信息。然后,该示例运行聚合管道:

const pipeline = [{ $indexStats: {} }];

const cursor = collection.aggregate(pipeline);
return cursor;