$indexStats
The $indexStats aggregation stage in Amazon DocumentDB provides insight into the usage of indexes within a collection. This operator allows you to monitor the access patterns of your indexes, which can help you make informed decisions about index management and optimization.
Parameters
None
Example (MongoDB Shell)
The following example demonstrates how to use the $indexStats operator to analyze the index usage in an Amazon DocumentDB collection.
Create sample documents
db.grocery.insertMany([ { _id: 1, product: "milk", quantity: 10 }, { _id: 2, product: "eggs", quantity: 20 }, { _id: 3, product: "bread", quantity: 5 }, { _id: 4, product: "cheese", quantity: 15 }, { _id: 5, product: "apple", quantity: 8 } ]);
Query example
db.grocery.aggregate([ { $indexStats: {} } ]);
Output
[
{
"name": "_id_",
"key": {
"_id": 1
},
"host": "docdb-cluster-1.cluster-123456789.us-west-2.docdb.amazonaws.com",
"accesses": {
"ops": NumberLong(5),
"since": ISODate("2023-04-06T12:34:56.789Z")
}
},
{
"name": "product_1",
"key": {
"product": 1
},
"host": "docdb-cluster-1.cluster-123456789.us-west-2.docdb.amazonaws.com",
"accesses": {
"ops": NumberLong(10),
"since": ISODate("2023-04-06T12:34:56.789Z")
}
}
]
In this example, the $indexStats operator shows that the _id_ index has been accessed 5 times, and the product_1 index has been accessed 10 times since the last reset or server reboot.
Code examples
To view a code example for using the $indexStats command, choose the tab for the language that you want to use: