- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Pipeline Aggregation Stages >
- $indexStats (aggregation)
$indexStats (aggregation)¶
On this page
Definition¶
-
$indexStats¶ New in version 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
indexStatsaction.The
$indexStatsstage takes an empty document and has the following syntax:The return document includes the following fields:
Output Field Description nameIndex name. keyIndex key specification. hostThe hostname and port of the mongodprocess.accessesStatistics on the index use:
opsis the number of operations that used the index.sinceis the time from which MongoDB gathered the statistics.
Statistics for an index will be reset on
mongodrestart or index drop and recreation.
Behavior¶
The statistics reported by the accesses field only includes index
access driven by user requests. It does not include internal operations like
deletion via TTL Indexes or chunk split and migration operations.
$indexStats must be the first stage in an aggregation pipeline.
Example¶
For example, a collection orders contains the following documents:
Create the following two indexes on the collection:
Run some queries against the collection:
To view statistics on the index use on the orders collection,
run the following aggregation operation:
The operation returns a document that contains usage statistics for each index: