$indexStats匯總階段會傳回集合中每個索引的使用方式統計數據。 此階段適用於分析索引效能、識別未使用的索引,以及優化查詢效能。
語法
{
$indexStats: {}
}
參數
階段 $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"
}
}
]