$indexStats

$indexStats聚合阶段返回集合中每个索引的使用情况统计信息。 此阶段可用于分析索引性能、识别未使用的索引以及优化查询性能。

Syntax

{
  $indexStats: {}
}

Parameters

阶段 $indexStats 不采用任何参数。

例子

请考虑商店集合中的这个示例文档。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

示例 1 - 提取索引统计信息

此查询检索存储集合上所有索引的使用情况统计信息。

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

查询返回每个索引的统计信息,包括访问模式和使用频率。

[
  {
    "name": "_id_",
    "key": { "_id": 1 },
    "accesses": { "ops": 41675, "since": "2025-06-07T13:51:41.231Z" },
    "spec": { "v": 2, "key": { "_id": 1 }, "name": "_id_" }
  },
  {
    "name": "location_2dsphere",
    "key": { "location": "2dsphere" },
    "accesses": { "ops": 0, "since": "2025-06-07T13:51:41.231Z" },
    "spec": {
      "v": 2,
      "key": { "location": "2dsphere" },
      "name": "location_2dsphere",
      "2dsphereIndexVersion": 3
    }
  },
  {
    "name": "name_text_sales.salesByCategory.categoryName_text_promotionEvents.eventName_text_promotionEvents.discounts.categoryName_text",
    "key": {
      "name": "text",
      "sales.salesByCategory.categoryName": "text",
      "promotionEvents.eventName": "text",
      "promotionEvents.discounts.categoryName": "text"
    },
    "accesses": { "ops": 8, "since": "2025-06-07T13:51:41.231Z" },
    "spec": {
      "v": 2,
      "key": {
        "name": "text",
        "sales.salesByCategory.categoryName": "text",
        "promotionEvents.eventName": "text",
        "promotionEvents.discounts.categoryName": "text"
      },
      "name": "name_text_sales.salesByCategory.categoryName_text_promotionEvents.eventName_text_promotionEvents.discounts.categoryName_text"
    }
  }
]