個別のフィールド値の取得
Overview
コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。 例、 restaurant
コレクション内の 1 つのドキュメントのborough
値は"Manhattan"
で、別のドキュメントのborough
値は"Queens"
です。 Java Reactive Streams ドライバーを使用すると、コレクション内の複数のドキュメントにわたってフィールドに含まれるすべての個別の値を検索できます。
サンプル データ
このガイドの例では、 Atlasサンプルデータセットの
sample_restaurants.restaurants
コレクションを使用します。 MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、開始 を参照してください。
重要
プロジェクトリ アクター ライブラリ
このガイドでは、プロジェクト Reactive ライブラリを使用して、 Java Reactive Streams ドライバー メソッドによって返されたPublisher
インスタンスを消費します。 Project Reactive ライブラリとその使用方法の詳細については、「 使用 開始 」を 参照してください。 (Reactor ドキュメントの参照)。このガイドでは Project React ライブラリ メソッドをどのように使用しているかについて詳しくは、「 MongoDBへのデータの書込み」ガイドを参照してください。
distinct()
方式
指定したフィールドの個別の値を検索するには、 distinct()
メソッドを呼び出し、個別の値を検索するフィールドの名前を渡します。
コレクション全体で個別の値を取得
次の例では、 restaurants
コレクション内のborough
フィールドの個別の値を取得します。
DistinctPublisher<String> distinctPublisher = collection .distinct("borough", String.class); Flux.from(distinctPublisher) .doOnNext(System.out::println) .blockLast();
Bronx Brooklyn Manhattan Missing Queens Staten Island
結果には、コレクション内のすべてのドキュメントにわたってborough
フィールドに表示されるすべての個別の値が表示されます。 borough
フィールドの値は複数のドキュメントで同じですが、各値は結果に 1 回だけ表示されます。
指定されたドキュメント全体で個別の値を取得
distinct()
メソッドにクエリフィルターを提供すると、コレクション内のドキュメントのサブセット全体で個別のフィールド値を検索できます。 クエリフィルター は、 操作内のドキュメントを照合するために使用される検索条件を指定する 式 です。 クエリフィルターの作成の詳細については、「クエリの指定 」を参照してください。
次の例では、 cuisine
フィールドの値が"Italian"
であるすべてのドキュメントのborough
フィールドの個別の値を取得します。
Bson filter = Filters.eq("cuisine", "Italian"); DistinctPublisher<String> distinctPublisher = collection .distinct("borough", String.class) .filter(filter); Flux.from(distinctPublisher) .doOnNext(System.out::println) .blockLast();
Bronx Brooklyn Manhattan Queens Staten Island
個別の動作の変更
distinct()
メソッドは、 distinct()
メソッド呼び出しにメソッドを連鎖させることで変更できます。 オプションを指定しない場合、ドライバーは操作をカスタマイズしません。
次の表では、 distinct()
操作をカスタマイズするために使用できるいくつかの方法について説明します。
方式 | 説明 |
---|---|
| Sets the number of documents to return per batch. By default, returns an initial batch size
of 101 documents and a maximum size of 16 mebibytes (MiB) for each subsequent batch.
This option can enforce a smaller limit than 16 MiB, but not a larger one.A batchSize of 0 means that the cursor will be established, but no documents
will be returned in the first batch. |
| Specifies the kind of language collation to use when sorting
results. For more information, see Collation
in the MongoDB Server manual. |
| Specifies a comment to attach to the operation. |
| Sets the query filter to apply to the query. |
| Sets the maximum amount of time to allow the operation to run, in milliseconds. |
distinct()
メソッドを変更するために使用できる方法の完全なリストについては、 DistinguishedPublisher を参照してください APIドキュメント。
次の例では、 borough
フィールド値が"Bronx"
で、かつcuisine
フィールド値が"Pizza"
であるすべてのドキュメントのname
フィールドの個別の値を取得します。 また、 comment
オプションを使用して操作にコメントを追加します。
Bson filter = Filters.and( Filters.eq("borough", "Bronx"), Filters.eq("cuisine", "Pizza") ); DistinctPublisher<String> distinctPublisher = collection .distinct("name", String.class) .filter(filter) .comment("Bronx pizza restaurants"); Flux.from(distinctPublisher) .doOnNext(System.out::println) .blockLast();
$1.25 Pizza 18 East Gunhill Pizza 2 Bros Aenos Pizza Alitalia Pizza Restaurant ...
詳細情報
コマンドの詳細については、 MongoDB Serverマニュアルの 個別の ガイド を参照してください。
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。