Docs Menu

Multikey Indexes

Multikey indexes are indexes that improve the performance of queries on array-valued fields. You can create a multikey index on a collection by using the MongoDB\Collection::createIndex() method and the same syntax that you use to create single field or compound indexes.

このガイドの例では、 Atlas サンプル データセットsample_mflixデータベースのmoviesコレクションを使用します。 MongoDB Atlas クラスターを無料で作成して、サンプル データセットをロードする方法については、 「 Atlas を使い始める 」ガイドを参照してください。

Use the MongoDB\Collection::createIndex() method to create a multikey index. The following example creates an index in ascending order on the array-valued cast field:

$indexName = $collection->createIndex(['cast' => 1]);

以下は、前のコード例で作成されたインデックスによってカバーされるクエリの例です。

$document = $collection->findOne(
['cast' => ['$in' => ['Aamir Khan', 'Kajol']]]
);
echo json_encode($document), PHP_EOL;
{"_id":...,"title":"Holi",...,"cast":["Ashutosh Gowariker",
"Aamir Khan","Rahul Ranade","Sanjeev Gandhi"],...}

Multikey indexes behave differently from other indexes in terms of query coverage, index bound computation, and sort behavior. To learn more about the behavior and limitations of multikey indexes, see Multikey Indexes in the MongoDB Server manual.

インデックスを管理する方法を示す実行可能な例については、「 インデックスを使用してクエリを最適化する 」を参照してください。

このガイドで説明されているメソッドの詳細については、次の API ドキュメントを参照してください。