AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

クエリ最適化のインデックス

このガイドでは、 Kotlin Sync ドライバーでインデックスを使用する方法を学習できます。 インデックスはクエリの効率を向上させ、ドキュメントのクエリと保存に機能を追加します。

インデックスがないと、MongoDB はコレクション内のすべてのドキュメントをスキャンして 、各クエリに一致するドキュメントを見つける必要があります。 これらのコレクションスキャンは遅く、アプリケーションのパフォーマンスに悪影響を与える可能性があります。 ただし、クエリに適切なインデックスがある場合、MongoDB はそのインデックスを使用して検査する必要があるドキュメントを制限できます。

To learn about the criteria your query must meet to return results directly from an index, see the Covered Query section in the MongoDB Server manual.

クエリのパフォーマンスを向上させるには、ソートされた結果を返すアプリケーションのクエリや操作で頻繁に表示されるフィールドにインデックスを構築します。追加する各インデックスは、アクティブになるとディスク容量とメモリを消費するため、キャパシティプランニングのためにインデックスメモリとディスク使用量を追跡する必要がある場合があります。さらに、書込み操作によってインデックス付きフィールドが更新されると、MongoDB は関連するインデックスも更新します。

Since MongoDB supports dynamic schemas, applications can query against fields whose names cannot be known in advance or are arbitrary. Wildcard indexes help support these queries, but are not designed to replace workload-based index planning.

データモデルの設計とアプリケーションに適したインデックスの選択の詳細については、サーバーのドキュメントの「インデックス作成戦略」と「クエリをサポートするインデックスの作成」を参照してください。

MongoDB には、データのクエリをサポートするためにさまざまなインデックスタイプがあります。コレクションで createIndex() メソッドを呼び出し、インデックス定義を引数として渡すことでインデックスを作成できます。

以下のセクションでは、最も一般的なインデックス型について説明し、各インデックス型を作成するためのサンプルコードを示します。

注意

The examples on this page use the sample_mflix.movies and sample_mflix.theaters collections from the Atlas sample datasets. To learn how to create a free MongoDB deployment and load the sample datasets, see Get Started with the Kotlin Sync Driver.

さまざまなインデックスタイプの詳細については、 MongoDB Serverマニュアルのインデックス タイプを参照してください。

次の例では、指定されたフィールドに昇順のインデックスを作成しています。

collection.createIndex(Indexes.ascending("<field name>"))

To learn more about single field indexes, see Single Field Indexes in the MongoDB Server manual.

次の例では、指定されたフィールドに複合インデックスを作成しています。

collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"))

To learn more about compound indexes, see Compound Indexes in the MongoDB Server manual.

次の例では、指定された配列値フィールドにマルチキー インデックスを作成します。

collection.createIndex(Indexes.ascending("<array field name>"))

To learn more about multikey indexes, see Multikey Indexes in the MongoDB Server manual.

次の例では、 _idフィールドにクラスター化インデックスを持つ新しいコレクションを作成します。

val clusteredIndexOptions = ClusteredIndexOptions(
Indexes.ascending("_id"),
true
)
val collection = database.createCollection("<collection name>", clusteredIndexOptions)

クラスター化されたコレクションとインデックスの詳細については、 MongoDB Serverマニュアルのクラスター化されたコレクションを参照してください。

次の例では、指定された string フィールドにテキスト インデックスを作成します。

collection.createIndex(Indexes.text("<field name>"))

次の例では、GeoJSON オブジェクトを含む指定されたフィールドに2dsphereインデックスを作成します。

collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"))

To learn more about geospatial indexes, see Geospatial Indexes in the MongoDB Server manual.

次の例では、指定されたフィールドに一意のインデックスを作成しています。

val indexOptions = IndexOptions().unique(true)
collection.createIndex(Indexes.ascending("<field name>"), indexOptions)

To learn more about unique indexes, see Unique Indexes in the MongoDB Server manual.

次の例では、 指定されたコレクションにワイルドカード インデックスを作成します。

collection.createIndex(Indexes.ascending("$**"))

ワイルドカード インデックスの詳細については、 MongoDB Serverマニュアルの「 ワイルドカード インデックス 」を参照してください。

次の例に示すように、コレクションで dropIndex() メソッドを使用してインデックスを削除できます。

collection.dropIndex("<index name>")