For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

$indexStats (aggregation stage)

$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.

The $indexStats stage takes an empty document and has the following syntax:

{ $indexStats: { } }

For each index, the return document includes the following fields:

Output Field
Description

name

Index name.

Index key specification.

See also: spec.

The hostname and port of the mongod process.

Index usage metrics for the associated host, including:

  • ops: the number of user operations that used the index. This value excludes internal operations, such as deletion by TTL indexes or chunk split and migration operations. The value also excludes index access for replication purposes, such as oplog application and index builds during initial sync.

  • since: the time from which MongoDB gathered the statistics

WARNING: The accesses field reports index usage metrics only for the node where the query runs. For more comprehensive index usage statistics, run $indexStats on each node in the cluster.

The name of the shard associated with the host

Only available for a sharded cluster.

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.

Indicates if the index is currently being built.

Only available if true.

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. The accesses field excludes the following internal operations:

  • Deletion by TTL indexes

  • Chunk split and migration operations

  • Index access from replication, such as oplog application and index builds during initial sync

  • $indexStats must be the first stage in an aggregation pipeline.

  • $indexStats is not allowed in transactions.

  • Statistics for an index reset on mongod restart or index drop and recreation.

  • Modification of an existing index (see collMod command) resets the statistics for that index.

The following example uses the movies collection from the sample_mflix sample dataset. For details on how to load this dataset into your deployment, see Load the sample dataset.

Create the following indexes on the movies collection:

db.movies.createIndex( { year: 1, title: 1 } )db.movies.createIndex( { rated: 1, year: 1 } )db.movies.createIndex( { metacritic: 1 }, { partialFilterExpression: { rated: "R" } })

Run some queries against the collection:

db.movies.find( { rated: "R" } )db.movies.find( { year: 1994 } ).sort( { title: 1 } )db.movies.find( { metacritic: { $gt: 90 } } )

The following aggregation operation returns a document that contains usage statistics for each index on the movies collection:

db.movies.aggregate( [ { $indexStats: { } } ] )
[ { name: 'year_1_title_1', key: { year: 1, title: 1 }, host: ..., accesses: ..., spec: { v: 2, key: { year: 1, title: 1 }, name: 'year_1_title_1' } }, { name: 'rated_1_year_1', key: { rated: 1, year: 1 }, host: ..., accesses: ..., spec: { v: 2, key: { rated: 1, year: 1 }, name: 'rated_1_year_1' } }, { name: 'metacritic_1', key: { metacritic: 1 }, host: ..., accesses: ..., spec: { v: 2, key: { metacritic: 1 }, name: 'metacritic_1', partialFilterExpression: { rated: 'R' } } }, { name: '_id_', key: { _id: 1 }, host: ..., accesses: ..., spec: { v: 2, key: { _id: 1 }, name: '_id_' } }, { name: 'cast_text_fullplot_text_genres_text_title_text', key: { _fts: 'text', _ftsx: 1 }, host: ..., accesses: ..., spec: ... }]
Rate this page