@@ -71,15 +71,15 @@ Filter the Events
71
71
~~~~~~~~~~~~~~~~~
72
72
73
73
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:
76
76
77
77
.. code-block:: java
78
78
79
- List<Bson> pipeline = Arrays.asList (
79
+ List<Bson> pipeline = List.of (
80
80
Aggregates.match(
81
81
Filters.in("operationType",
82
- Arrays.asList ("insert", "update"))),
82
+ List.of ("insert", "update"))),
83
83
Aggregates.match(
84
84
Filters.lt("fullDocument.runtime", 15)));
85
85
ChangeStreamIterable<Document> changeStream = database.watch(pipeline);
@@ -92,38 +92,42 @@ transform the change event output as follows:
92
92
by calling the ``fullDocument()`` member method of the ``ChangeStreamIterable``
93
93
object with the value ``FullDocument.UPDATE_LOOKUP`` as follows:
94
94
95
+ .. code-block:: java
96
+
97
+ ChangeStreamIterable<Document> changeStream = database.watch()
98
+ .fullDocument(FullDocument.UPDATE_LOOKUP);
99
+
95
100
Manage the Output
96
101
~~~~~~~~~~~~~~~~~
97
102
98
- The ``watch()`` method returns an instance of ``ChangeStreamIterable``, a class
103
+ The ``watch()`` method returns an instance of ``ChangeStreamIterable``, an interface
99
104
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 ,
101
106
``MongoIterable`` which implements the core Java interface ``Iterable``.
102
107
103
108
You can call ``forEach()`` on the ``ChangeStreamIterable`` to handle
104
109
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.
108
111
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`:
116
113
117
114
- ``hasNext()``: checks if there are more results.
118
115
- ``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
120
117
change stream or ``null``.
121
118
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``.
127
131
128
132
To configure options for processing the documents returned from the change
129
133
stream, use member methods of the ``ChangeStreamIterable`` object returned
@@ -198,8 +202,7 @@ Full File Example Output
198
202
199
203
The preceding applications will generate the following output:
200
204
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:
203
206
204
207
.. code-block::
205
208
:copyable: false
@@ -319,7 +322,7 @@ The following example modifies your change stream by using the
319
322
.. code-block:: java
320
323
321
324
ChangeStreamIterable<Document> changeStream = collection.watch(
322
- Arrays.asList (Document.parse("{ $changeStreamSplitLargeEvent: {} }")));
325
+ List.of (Document.parse("{ $changeStreamSplitLargeEvent: {} }")));
323
326
324
327
.. note::
325
328
0 commit comments