Docs Menu
Docs Home
/ / /
Java Reactive Streams ドライバー

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

項目一覧

  • Overview
  • プロジェクトリ アクターの実装
  • サンプル アプリケーション
  • 単一フィールド インデックス
  • 複合インデックス
  • Multikey Index
  • 地理空間インデックス
  • ユニークインデックス
  • ワイルドカード インデックス
  • クラスター化されたインデックス
  • Atlas Searchインデックス マネジメント
  • 検索インデックスを作成
  • 検索インデックスをリストする
  • 検索インデックスを更新
  • 検索インデックスを削除
  • Text Index
  • インデックスの削除

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

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

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

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

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

重要

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

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

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

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

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

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

戻る

データの変更を監視