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

インデックスを使用したクエリの最適化

このページでは、 Java Reactive Streams ドライバーを使用してさまざまなタイプのインデックスを管理する方法を示すコピー可能なコード例を紹介します。

このページの例を使用するには、コード サンプルをサンプル アプリケーションまたは独自のアプリケーションにコピーします。 <connection string URI>などのコード例にあるすべてのプレースホルダーを、MongoDB 配置に関連する値に置き換えてください。

このガイドでは、プロジェクト Reactive ライブラリを使用して、 Java Reactive Streams ドライバー メソッドによって返された Publisher インスタンスを消費します。Project Reactive ライブラリとその使用方法の詳細については、React ドキュメントの使用開始を参照してください。

Publisher インスタンスを消費する他の方法もあります。RxJava などの多数の代替ライブラリの 1 つを使用するか、Publisher.subscribe() を直接呼び出して Subscriber の独自実装を渡すことができます。

このガイドでは、React の Mono.block() メソッドを使用して Publisher をサブスクライブし、Publisher がターミナル状態に達するまで現在のスレッドをブロックします。Reactive Streams イベントの詳細については、「Reactive Streams 」を参照してください。

重要

返された出版社はコールド

Java Reactive Streams ドライバー メソッドによって返されるすべてのPublisherインスタンスはコールドです。つまり、返されたPublisherをサブスクライブしないと、対応する操作は実行されません。 返されたPublisherを 1 回だけサブスクライブすることをお勧めします。複数回サブスクライブするとエラーが発生する可能性があるためです。

次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。

  1. IDE で新しいJavaプロジェクトを作成します。

  2. Java Reactive Streams ドライバーをJavaプロジェクトにインストールします。

  3. Project Reactive ライブラリをJavaプロジェクトにインストールします。

  4. 次のコードをコピーし、 IndexApp.javaという名前の新しいJavaファイルに貼り付けます。

  5. このページからコード例をコピーし、 ファイル内の指定された行に貼り付けます。

import com.mongodb.ConnectionString;import com.mongodb.MongoClientSettings;import com.mongodb.ServerApi;import com.mongodb.ServerApiVersion; import com.mongodb.client.model.ClusteredIndexOptions;import com.mongodb.client.model.CreateCollectionOptions;import com.mongodb.client.model.IndexOptions;import com.mongodb.client.model.Indexes;import com.mongodb.reactivestreams.client.*;import org.bson.Document;import org.reactivestreams.Publisher;import reactor.core.publisher.Flux;import reactor.core.publisher.Mono; public class IndexApp { public static void main(String[] args) { // Replace the placeholder with your Atlas connection string String uri = "<connection string URI>"; // Construct a ServerApi instance using the ServerApi.builder() method ServerApi serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build(); MongoClientSettings settings = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(uri)) .serverApi(serverApi) .build(); // Create a new client and connect to the server try (MongoClient mongoClient = MongoClients.create(settings)) { MongoDatabase database = mongoClient.getDatabase("<database name>"); MongoCollection<Document> collection = database.getCollection("<collection name>"); // Start example code here // End example code here } }}

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

Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name>"));Mono.from(publisher).block();

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

Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"));Mono.from(publisher).block();

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

Publisher<String> publisher = collection.createIndex(Indexes.ascending("<array field name>"));Mono.from(publisher).block();

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

Publisher<String> publisher = collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"));Mono.from(publisher).block();

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

IndexOptions indexOptions = new IndexOptions().unique(true);Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name>"), indexOptions);Mono.from(publisher).block();

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

Publisher<String> publisher = collection.createIndex(Indexes.ascending("$**"));Mono.from(publisher).block();

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

ClusteredIndexOptions clusteredIndexOptions = new ClusteredIndexOptions( Indexes.ascending("_id"), true); CreateCollectionOptions createCollectionOptions= new CreateCollectionOptions() .clusteredIndexOptions(clusteredIndexOptions); Publisher<Void> clusteredCollection = database.createCollection("<collection name>", createCollectionOptions);Mono.from(clusteredCollection).block();

次のセクションには、MongoDB 検索インデックスを管理する方法を説明するコード例が含まれています。

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

Document index = new Document("mappings", new Document("dynamic", true));Publisher<String> publisher = collection.createSearchIndex("<index name>", index);Mono.from(publisher).block();

次の例では、指定されたコレクション内のMongoDB 検索インデックスのリストを出力します。

ListSearchIndexesPublisher<Document> listIndexesPublisher = collection.listSearchIndexes(); Flux.from(listIndexesPublisher) .doOnNext(System.out::println) .blockLast();

次の例では、指定された新しいインデックス定義で既存のMongoDB検索インデックスを更新します。

Document newIndex = new Document("mappings", new Document("dynamic", true));Publisher<Void> publisher = collection.updateSearchIndex("<index name>", newIndex);Mono.from(publisher).block();

次の例では、指定された名前のMongoDB検索インデックスを削除します。

Publisher<Void> publisher = collection.dropIndex("<index name>");Mono.from(publisher).block();

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

Publisher<String> publisher = collection.createIndex(Indexes.text("<field name>"));Mono.from(publisher).block();

次の例では、指定された名前のインデックスを 1 つ削除します。

Publisher<Void> publisher = collection.dropIndex("<index name>");Mono.from(publisher).block();