Skip to content

Commit 32dcdcd

Browse files
authored
DOCSP-39721: Distinct (#73)
1 parent 441c0c8 commit 32dcdcd

File tree

3 files changed

+232
-2
lines changed

3 files changed

+232
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import com.mongodb.ConnectionString;
2+
import com.mongodb.MongoClientSettings;
3+
import com.mongodb.client.model.Filters;
4+
import com.mongodb.reactivestreams.client.*;
5+
import org.bson.Document;
6+
import org.bson.conversions.Bson;
7+
import reactor.core.publisher.Flux;
8+
9+
public class Distinct {
10+
public static void main(String[] args) {
11+
String uri = "<connection string URI>";
12+
13+
MongoClientSettings settings = MongoClientSettings.builder()
14+
.applyConnectionString(new ConnectionString(uri))
15+
.build();
16+
17+
try (MongoClient mongoClient = MongoClients.create(settings))
18+
{
19+
MongoDatabase database = mongoClient.getDatabase("sample_restaurants");
20+
MongoCollection<Document> collection = database.getCollection("restaurants");
21+
22+
// start-distinct
23+
DistinctPublisher<String> distinctPublisher = collection
24+
.distinct("borough", String.class);
25+
26+
Flux.from(distinctPublisher)
27+
.doOnNext(System.out::println)
28+
.blockLast();
29+
// end-distinct
30+
31+
// start-distinct-query
32+
Bson filter = Filters.eq("cuisine", "Italian");
33+
34+
DistinctPublisher<String> distinctPublisher = collection
35+
.distinct("borough", String.class)
36+
.filter(filter);
37+
38+
Flux.from(distinctPublisher)
39+
.doOnNext(System.out::println)
40+
.blockLast();
41+
// end-distinct-query
42+
43+
// start-distinct-comment
44+
Bson filter = Filters.and(
45+
Filters.eq("borough", "Bronx"),
46+
Filters.eq("cuisine", "Pizza")
47+
);
48+
49+
DistinctPublisher<String> distinctPublisher = collection
50+
.distinct("name", String.class)
51+
.filter(filter)
52+
.comment("Bronx pizza restaurants");
53+
54+
Flux.from(distinctPublisher)
55+
.doOnNext(System.out::println)
56+
.blockLast();
57+
// end-distinct-comment
58+
}
59+
}
60+
}

source/read-data-from-mongo.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Read Data From MongoDB
2727
/read/specify-fields-return
2828
/read/specify-documents-to-return
2929
/read/count-documents
30+
/read/distinct
3031
/read/read-preference
3132
/read/text-search
3233
/read/geo
@@ -166,8 +167,7 @@ collection:
166167
:language: java
167168
:copyable:
168169

169-
To learn more about the ``distinct()`` method, see :manual:`distinct()
170-
<reference/method/db.collection.distinct/>` in the {+mdb-server+} manual.
170+
To learn more about the ``distinct()`` method, see the :ref:`<java-rs-distinct>` guide.
171171

172172
Monitor Data Changes
173173
--------------------

source/read/distinct.txt

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
.. _java-rs-distinct:
2+
3+
==============================
4+
Retrieve Distinct Field Values
5+
==============================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: read, unique, code example
19+
20+
Overview
21+
--------
22+
23+
Within a collection, different documents might contain different values for a single field.
24+
For example, one document in the ``restaurant`` collection has a ``borough`` value of ``"Manhattan"``, and
25+
another has a ``borough`` value of ``"Queens"``. With the {+driver-short+}, you can
26+
retrieve all the distinct values that a field contains across multiple documents
27+
in a collection.
28+
29+
Sample Data
30+
~~~~~~~~~~~
31+
32+
The examples in this guide use the ``sample_restaurants.restaurants`` collection
33+
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
34+
free MongoDB Atlas cluster and load the sample datasets, see
35+
:ref:`<java-rs-getting-started>`.
36+
37+
.. include:: includes/reactor-note.rst
38+
39+
``distinct()`` Method
40+
---------------------
41+
42+
To retrieve the distinct values for a specified field, call the ``distinct()``
43+
method and pass in the name of the field you want to find distinct values for.
44+
45+
Retrieve Distinct Values Across a Collection
46+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47+
48+
The following example retrieves the distinct values of the ``borough`` field in
49+
the ``restaurants`` collection:
50+
51+
.. io-code-block::
52+
53+
.. input:: /includes/read-ops/distinct.java
54+
:start-after: start-distinct
55+
:end-before: end-distinct
56+
:language: java
57+
:dedent:
58+
59+
.. output::
60+
61+
Bronx
62+
Brooklyn
63+
Manhattan
64+
Missing
65+
Queens
66+
Staten Island
67+
68+
The results show every distinct value that appears in the ``borough`` field
69+
across all documents in the collection. Although several documents have the same
70+
value in the ``borough`` field, each value appears in the results only once.
71+
72+
Retrieve Distinct Values Across Specified Documents
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
You can provide a **query filter** to the ``distinct()`` method to find the distinct
76+
field values across a subset of documents in a collection. A query filter is an expression that specifies search
77+
criteria used to match documents in an operation. For more information about
78+
creating a query filter, see :ref:`java-rs-specify-query`.
79+
80+
The following example retrieves the distinct values of the ``borough`` field for
81+
all documents that have a ``cuisine`` field value of ``"Italian"``:
82+
83+
.. io-code-block::
84+
85+
.. input:: /includes/read-ops/distinct.java
86+
:start-after: start-distinct-query
87+
:end-before: end-distinct-query
88+
:language: java
89+
:dedent:
90+
91+
.. output::
92+
93+
Bronx
94+
Brooklyn
95+
Manhattan
96+
Queens
97+
Staten Island
98+
99+
Modify Distinct Behavior
100+
~~~~~~~~~~~~~~~~~~~~~~~~
101+
102+
The ``distinct()`` method can be modified by chaining methods to the ``distinct()`` method
103+
call. If you don't specify any options, the driver does not customize the operation.
104+
105+
The following table describes some methods you can use to customize the
106+
``distinct()`` operation:
107+
108+
.. list-table::
109+
:widths: 30 70
110+
:header-rows: 1
111+
112+
* - Method
113+
- Description
114+
115+
* - ``batchSize()``
116+
- | Sets the number of documents to return per batch.
117+
118+
* - ``collation()``
119+
- | Specifies the kind of language collation to use when sorting
120+
results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>`
121+
in the {+mdb-server+} manual.
122+
123+
* - ``comment()``
124+
- | Specifies a comment to attach to the operation.
125+
126+
* - ``filter()``
127+
- | Sets the query filter to apply to the query.
128+
129+
* - ``maxTime()``
130+
- | Sets the maximum amount of time to allow the operation to run, in milliseconds.
131+
132+
For a complete list of methods you can use to modify the ``distinct()`` method, see
133+
the `DistinctPublisher <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/DistinctPublisher.html>`__ API documentation.
134+
135+
The following example retrieves the distinct values of the ``name`` field for
136+
all documents that have a ``borough`` field value of ``"Bronx"`` and a
137+
``cuisine`` field value of ``"Pizza"``. It also uses
138+
the ``comment`` option to add a comment to the operation.
139+
140+
.. io-code-block::
141+
142+
.. input:: /includes/read-ops/distinct.java
143+
:start-after: start-distinct-comment
144+
:end-before: end-distinct-comment
145+
:language: java
146+
:dedent:
147+
148+
.. output::
149+
150+
$1.25 Pizza
151+
18 East Gunhill Pizza
152+
2 Bros
153+
Aenos Pizza
154+
Alitalia Pizza Restaurant
155+
...
156+
157+
Additional Information
158+
----------------------
159+
160+
To learn more about the distinct command, see the :manual:`Distinct guide
161+
</reference/command/distinct/>` in the MongoDB Server Manual.
162+
163+
API Documentation
164+
~~~~~~~~~~~~~~~~~
165+
166+
To learn more about any of the methods or types discussed in this
167+
guide, see the following API documentation:
168+
169+
- `distinct() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#distinct(java.lang.String,java.lang.Class)>`__
170+
- `DistinctPublisher <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/DistinctPublisher.html>`__

0 commit comments

Comments
 (0)