Docs Menu

個別のフィールド値の取得

コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。 例、 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()メソッドを呼び出し、個別の値を検索するフィールドの名前を渡します。

次の例では、 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()操作をカスタマイズするために使用できるいくつかの方法について説明します。

方式
説明

batchSize()

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.

collation()

Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.

comment()

Specifies a comment to attach to the operation.

filter()

Sets the query filter to apply to the query.

maxTime()

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 ドキュメントを参照してください。