Docs Menu

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

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

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

このガイドでは、プロジェクト Reactive ライブラリを使用して、 Java Reactive Streams ドライバー メソッドによって返された Publisher インスタンスを消費します。 プロジェクト Reactor ライブラリとその使用方法の詳細については、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 React ライブラリ をインストール Javaプロジェクト。

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

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

1import com.mongodb.ConnectionString;
2import com.mongodb.MongoClientSettings;
3import com.mongodb.ServerApi;
4import com.mongodb.ServerApiVersion;
5
6import com.mongodb.client.model.ClusteredIndexOptions;
7import com.mongodb.client.model.CreateCollectionOptions;
8import com.mongodb.client.model.IndexOptions;
9import com.mongodb.client.model.Indexes;
10import com.mongodb.reactivestreams.client.*;
11import org.bson.Document;
12import org.reactivestreams.Publisher;
13import reactor.core.publisher.Flux;
14import reactor.core.publisher.Mono;
15
16public class IndexApp {
17 public static void main(String[] args) {
18 // Replace the placeholder with your Atlas connection string
19 String uri = "<connection string URI>";
20
21 // Construct a ServerApi instance using the ServerApi.builder() method
22 ServerApi serverApi = ServerApi.builder()
23 .version(ServerApiVersion.V1)
24 .build();
25
26 MongoClientSettings settings = MongoClientSettings.builder()
27 .applyConnectionString(new ConnectionString(uri))
28 .serverApi(serverApi)
29 .build();
30
31 // Create a new client and connect to the server
32 try (MongoClient mongoClient = MongoClients.create(settings)) {
33 MongoDatabase database = mongoClient.getDatabase("<database name>");
34 MongoCollection<Document> collection = database.getCollection("<collection name>");
35
36 // Start example code here
37
38 // End example code here
39 }
40 }
41}

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

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();

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

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

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

次の例では、指定されたコレクション内の Atlas Search インデックスの一覧を出力します。

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

次の例では、指定された新しいインデックス定義で既存の Atlas Search インデックスをアップデートします。

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

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

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();