インデックスを使用したクエリの最適化
項目一覧
Overview
このページでは、 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 回だけサブスクライブすることをお勧めします。複数回サブスクライブするとエラーが発生する可能性があるためです。
サンプル アプリケーション
次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。
IDE で新しいJavaプロジェクトを作成します。
Java Reactive Streams ドライバーをJavaプロジェクトにインストールします。
Project React ライブラリ をインストール Javaプロジェクト。
次のコードをコピーし、
IndexApp.java
という名前の新しいJavaファイルに貼り付けます。このページからコード例をコピーし、 ファイル内の指定された行に貼り付けます。
1 import com.mongodb.ConnectionString; 2 import com.mongodb.MongoClientSettings; 3 import com.mongodb.ServerApi; 4 import com.mongodb.ServerApiVersion; 5 6 import com.mongodb.client.model.ClusteredIndexOptions; 7 import com.mongodb.client.model.CreateCollectionOptions; 8 import com.mongodb.client.model.IndexOptions; 9 import com.mongodb.client.model.Indexes; 10 import com.mongodb.reactivestreams.client.*; 11 import org.bson.Document; 12 import org.reactivestreams.Publisher; 13 import reactor.core.publisher.Flux; 14 import reactor.core.publisher.Mono; 15 16 public 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();
Multikey Index
次の例では、指定された配列値フィールドにマルチキー インデックスを作成します。
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 インデックスを管理する方法を説明するコード例が含まれています。
検索インデックスを作成
次の例では、指定されたフィールドに 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();
Text Index
次の例では、指定された 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();