$indexStats - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$indexStats

Amazon DocumentDB 中的$indexStats聚合阶段可以深入了解集合中索引的使用情况。此运算符允许您监控索引的访问模式,这可以帮助您就索引管理和优化做出明智的决策。

参数

示例(MongoDB 外壳)

以下示例演示如何使用$indexStats运算符来分析 Amazon DocumentDB 集合中的索引使用情况。

创建示例文档

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 } ]);

查询示例

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

输出

[ { "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") } } ]

在此示例中,$indexStats运算符显示自上次重置或服务器重启以来,product_1索引已被访问 5 次,索引已被访问 10 次。_id_

代码示例

要查看使用该$indexStats命令的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function indexStats() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const result = await db.collection('grocery').aggregate([ { $indexStats: {} } ]).toArray(); console.log(result); await client.close(); } indexStats();
Python
from pymongo import MongoClient def index_stats(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] result = list(db.grocery.aggregate([ { '$indexStats': {} } ])) print(result) client.close() index_stats()