Retrieve Distinct Field Values
On this page
Overview
Within a collection, different documents might contain different values for a single field.
For example, one document in the restaurant
collection has a borough
value of "Manhattan"
, and
another has a borough
value of "Queens"
. With the Java Reactive Streams driver, you can
retrieve all the distinct values that a field contains across multiple documents
in a collection.
Sample Data
The examples in this guide use the sample_restaurants.restaurants
collection
from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see
Get Started.
Important
Project Reactor Library
This guide uses the Project Reactor library to consume Publisher
instances returned
by the Java Reactive Streams driver methods. To learn more about the Project Reactor library
and how to use it, see Getting Started
in the Reactor documentation. To learn more about how we use Project Reactor
library methods in this guide, see the Write Data to MongoDB guide.
distinct()
Method
To retrieve the distinct values for a specified field, call the distinct()
method and pass in the name of the field you want to find distinct values for.
Retrieve Distinct Values Across a Collection
The following example retrieves the distinct values of the borough
field in
the restaurants
collection:
DistinctPublisher<String> distinctPublisher = collection .distinct("borough", String.class); Flux.from(distinctPublisher) .doOnNext(System.out::println) .blockLast();
Bronx Brooklyn Manhattan Missing Queens Staten Island
The results show every distinct value that appears in the borough
field
across all documents in the collection. Although several documents have the same
value in the borough
field, each value appears in the results only once.
Retrieve Distinct Values Across Specified Documents
You can provide a query filter to the distinct()
method to find the distinct
field values across a subset of documents in a collection. A query filter is an expression that specifies search
criteria used to match documents in an operation. For more information about
creating a query filter, see Specify a Query.
The following example retrieves the distinct values of the borough
field for
all documents that have a cuisine
field value of "Italian"
:
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
Modify Distinct Behavior
The distinct()
method can be modified by chaining methods to the distinct()
method
call. If you don't specify any options, the driver does not customize the operation.
The following table describes some methods you can use to customize the
distinct()
operation:
Method | Description |
---|---|
batchSize() | Sets the number of documents to return per 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. |
For a complete list of methods you can use to modify the distinct()
method, see
the DistinctPublisher API documentation.
The following example retrieves the distinct values of the name
field for
all documents that have a borough
field value of "Bronx"
and a
cuisine
field value of "Pizza"
. It also uses
the comment
option to add a comment to the operation.
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 ...
Additional Information
To learn more about the distinct command, see the Distinct guide in the MongoDB Server Manual.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: