$indexStats

The `$indexStats` aggregation stage returns usage statistics for each index in the collection. This stage is useful for analyzing index performance, identifying unused indexes, and optimizing query performance.

Syntax

{
  $indexStats: {}
}

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "name": "First Up Consultants | Beverage Shop",
  "sales": { "totalSales": 75670 }
}

Get index usage statistics

Return usage statistics for all indexes in the stores collection.

This query retrieves index usage information for performance analysis.

Query:

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

Output:

[
  {
    "name": "_id_",
    "key": { "_id": 1 },
    "host": "host:port",
    "accesses": {
      "ops": 1234,
      "since": "2024-01-01T00:00:00.000Z"
    }
  }
]

Related