

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# \$1indexStats
<a name="indexStats"></a>

Amazon DocumentDB の`$indexStats`集約ステージは、コレクション内のインデックスの使用に関するインサイトを提供します。この演算子を使用すると、インデックスのアクセスパターンをモニタリングできるため、インデックスの管理と最適化について情報に基づいた意思決定を行うことができます。

**パラメータ**

なし

## 例 (MongoDB シェル)
<a name="indexStats-examples"></a>

次の例は、 `$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`演算子は、`_id_`インデックスが 5 回アクセスされ、`product_1`インデックスが前回のリセットまたはサーバー再起動から 10 回アクセスされたことを示しています。

## コードの例
<a name="indexStats-code"></a>

`$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()
```

------