Multikey Indexes
Overview
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 を使い始める 」ガイドを参照してください。
Create a Multikey Index
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 ドキュメント
このガイドで説明されているメソッドの詳細については、次の API ドキュメントを参照してください。