Skip to content

Commit 560672c

Browse files
committed
JY feedback
1 parent 890146e commit 560672c

File tree

4 files changed

+44
-53
lines changed

4 files changed

+44
-53
lines changed

source/crud/read-operations/distinct.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Overview
2424
In this guide, you can learn how to retrieve a list of distinct values for a
2525
field across a collection by calling the ``distinct()`` method on a
2626
``MongoCollection`` object. Pass the document field name as the first parameter
27-
and the class you want to cast the results to as the second parameter as shown
27+
and the class you want to use for the results format as the second parameter as shown
2828
below:
2929

3030
.. code-block:: java
@@ -64,7 +64,7 @@ match movies that include "Carl Franklin" as one of the values in the
6464

6565
.. include:: /includes/connect-guide-note.rst
6666

67-
.. literalinclude:: /includes/usage-examples/code-snippets/Distinct.java
67+
.. literalinclude:: /includes/crud/Distinct.java
6868
:language: java
6969

7070
When you run the example, you should see output that reports each distinct

source/includes/crud/CountDocuments.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,13 @@ public static void main(String[] args) {
2929

3030
Bson query = eq("countries", "Spain");
3131

32-
try {
33-
// Retrieves and prints the estimated number of documents in the collection
34-
long estimatedCount = collection.estimatedDocumentCount();
35-
System.out.println("Estimated number of documents in the movies collection: " + estimatedCount);
36-
37-
// Retrieves and prints the number of documents with a "countries" value of "Spain"
38-
long matchingCount = collection.countDocuments(query);
39-
System.out.println("Number of movies from Spain: " + matchingCount);
40-
41-
// Prints a message if any exceptions occur during the operations
42-
} catch (MongoException me) {
43-
System.err.println("An error occurred: " + me);
44-
}
32+
// Retrieves and prints the estimated number of documents in the collection
33+
long estimatedCount = collection.estimatedDocumentCount();
34+
System.out.println("Estimated number of documents in the movies collection: " + estimatedCount);
35+
36+
// Retrieves and prints the number of documents with a "countries" value of "Spain"
37+
long matchingCount = collection.countDocuments(query);
38+
System.out.println("Number of movies from Spain: " + matchingCount);
4539
}
4640
}
4741
}

source/includes/crud/Distinct.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,13 @@ public static void main(String[] args) {
2222
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2323
MongoCollection<Document> collection = database.getCollection("movies");
2424

25-
try {
26-
// Retrieves the distinct values of the "year" field present in documents that match the filter
27-
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
28-
MongoCursor<Integer> results = docs.iterator();
29-
30-
// Prints the distinct "year" values
31-
while(results.hasNext()) {
32-
System.out.println(results.next());
33-
}
34-
35-
// Prints a message if any exceptions occur during the operation
36-
} catch (MongoException me) {
37-
System.err.println("An error occurred: " + me);
25+
// Retrieves the distinct values of the "year" field present in documents that match the filter
26+
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
27+
MongoCursor<Integer> results = docs.iterator();
28+
29+
// Prints the distinct "year" values
30+
while(results.hasNext()) {
31+
System.out.println(results.next());
3832
}
3933
}
4034
}

source/logging-monitoring/change-streams.txt

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ Filter the Events
7171
~~~~~~~~~~~~~~~~~
7272

7373
The ``watch()`` method optionally takes an **aggregation pipeline** which
74-
consists of an array of **stages** as the first parameter to filter and
75-
transform the change event output as follows:
74+
consists of a list of **stages** as the first parameter, which can be used to
75+
filter and transform the change event output, as follows:
7676

7777
.. code-block:: java
7878

79-
List<Bson> pipeline = Arrays.asList(
79+
List<Bson> pipeline = List.of(
8080
Aggregates.match(
8181
Filters.in("operationType",
82-
Arrays.asList("insert", "update"))),
82+
List.of("insert", "update"))),
8383
Aggregates.match(
8484
Filters.lt("fullDocument.runtime", 15)));
8585
ChangeStreamIterable<Document> changeStream = database.watch(pipeline);
@@ -92,38 +92,42 @@ transform the change event output as follows:
9292
by calling the ``fullDocument()`` member method of the ``ChangeStreamIterable``
9393
object with the value ``FullDocument.UPDATE_LOOKUP`` as follows:
9494

95+
.. code-block:: java
96+
97+
ChangeStreamIterable<Document> changeStream = database.watch()
98+
.fullDocument(FullDocument.UPDATE_LOOKUP);
99+
95100
Manage the Output
96101
~~~~~~~~~~~~~~~~~
97102

98-
The ``watch()`` method returns an instance of ``ChangeStreamIterable``, a class
103+
The ``watch()`` method returns an instance of ``ChangeStreamIterable``, an interface
99104
that offers several methods to access, organize, and traverse the results.
100-
``ChangeStreamIterable`` also inherits methods from its parent class,
105+
``ChangeStreamIterable`` also inherits methods from its parent interface,
101106
``MongoIterable`` which implements the core Java interface ``Iterable``.
102107

103108
You can call ``forEach()`` on the ``ChangeStreamIterable`` to handle
104109
events as they occur, or you can use the ``iterator()`` method which
105-
returns a ``MongoCursor`` instance that you can use to traverse the results.
106-
107-
.. important:: forEach() blocks the current thread
110+
returns a ``MongoChangeStreamCursor`` instance that you can use to traverse the results.
108111

109-
Calls to ``forEach()`` block the current thread while the
110-
corresponding change stream listens for events. If your program
111-
needs to continue executing other logic, such as processing requests or
112-
responding to user input, consider creating and listening to your
113-
change stream in a separate thread.
114-
115-
You can call the following methods on the ``MongoCursor`:
112+
You can call the following methods on the ``MongoChangeStreamCursor`:
116113

117114
- ``hasNext()``: checks if there are more results.
118115
- ``next()`` returns the next document in the collection.
119-
- ``tryNext()`` immediately returns eitherthe next available element in the
116+
- ``tryNext()`` immediately returns either the next available element in the
120117
change stream or ``null``.
121118

122-
Unlike the ``MongoCursor`` returned by other queries, a ``MongoCursor`` associated
123-
with a change stream waits until a change event arrives before
124-
returning a result from ``next()``. As a result, calls to ``next()``
125-
using a change stream's ``MongoCursor`` never throw a
126-
``java.util.NoSuchElementException``.
119+
.. important:: Iterating the Cursor Blocks the Current Thread
120+
121+
Iterating through a cursor using ``forEach()`` or any ``iterator()`` method
122+
blocks the current thread while the corresponding change stream listens for
123+
events. If your program needs to continue executing other logic, such as
124+
processing requests or responding to user input, consider creating and
125+
listening to your change stream in a separate thread.
126+
127+
Unlike the ``MongoCursor`` returned by other queries, a
128+
``MongoChangeStreamCursor`` associated with a change stream waits until a change
129+
event arrives before returning a result from ``next()``. As a result, calls to
130+
``next()`` using a change stream's ``MongoChangeStreamCursor`` never throw a ``java.util.NoSuchElementException``.
127131

128132
To configure options for processing the documents returned from the change
129133
stream, use member methods of the ``ChangeStreamIterable`` object returned
@@ -198,8 +202,7 @@ Full File Example Output
198202

199203
The preceding applications will generate the following output:
200204

201-
``Watch.java`` will capture on the ``insert`` and ``update`` operations are
202-
printed, since the aggregation pipeline filters out the ``delete`` operation:
205+
``Watch.java`` will capture only the ``insert`` and ``update`` operations, since the aggregation pipeline filters out the ``delete`` operation:
203206

204207
.. code-block::
205208
:copyable: false
@@ -319,7 +322,7 @@ The following example modifies your change stream by using the
319322
.. code-block:: java
320323

321324
ChangeStreamIterable<Document> changeStream = collection.watch(
322-
Arrays.asList(Document.parse("{ $changeStreamSplitLargeEvent: {} }")));
325+
List.of(Document.parse("{ $changeStreamSplitLargeEvent: {} }")));
323326

324327
.. note::
325328

0 commit comments

Comments
 (0)