@@ -19,9 +19,11 @@ Overview
1919In this guide, you can learn how to use a **change stream** to monitor
2020real-time changes to your database. A change stream is a {+mdb-server+}
2121feature that allows your application to subscribe to data changes on a single
22- collection, database, or deployment. You can specify a set of aggregation
22+ collection, database, or deployment.
23+
24+ You can specify a set of aggregation
2325operators to filter and transform the data your application receives. When
24- connecting to a MongoDB deployment v6.0 or later, you can configure the
26+ connecting to a MongoDB deployment v6.0 or later, you can also configure the
2527events to include the document data before and after the change.
2628
2729Learn how to open and configure your change streams in the following
@@ -78,15 +80,14 @@ types. As a result, the driver stores individual change stream events as
7880 :start-after: begin openChangeStreamExample
7981 :end-before: end openChangeStreamExample
8082
81- An insert operation on the collection should produce output similar to the
82- following text:
83+ An insert operation on the collection produces the following output:
8384
8485.. code-block::
8586 :copyable: false
8687
8788 Received a change: ChangeStreamDocument{
88- operationType=' insert' ,
89- resumeToken={"_data": "825EC ..."},
89+ operationType=insert,
90+ resumeToken={"_data": "..."},
9091 namespace=myDb.myColl,
9192 ...
9293 }
@@ -111,7 +112,6 @@ to specify which change events the change stream receives.
111112To learn which aggregation operators your {+mdb-server+} version supports, see
112113:ref:`<change-stream-modify-output>`.
113114
114-
115115Example
116116~~~~~~~
117117
@@ -125,15 +125,17 @@ update operations:
125125 :start-after: begin aggregationExample
126126 :end-before: end aggregationExample
127127
128- When the change stream receives an update change event, the preceding code
129- example outputs the following text:
128+ An update operation on the collection produces the following output:
130129
131- .. code-block:: text
130+ .. code-block::
132131 :copyable: false
133132
134- Received a change to the collection: ChangeStreamDocument{
135- operationType=update, resumeToken={...},
136- ...
133+ Received a change: ChangeStreamDocument{
134+ operationType=update,
135+ resumeToken={"_data": "..."},
136+ namespace=myDb.myColl,
137+ ...
138+ }
137139
138140.. _java-change-stream-configure-pre-post:
139141
@@ -142,22 +144,27 @@ Include Pre-images and Post-images
142144
143145You can configure the change event to contain or omit the following data:
144146
145- - The **pre-image** which is a document that represents the version of the
146- document before the operation if it exists
147- - The **post-image** which is a document that represents the version of the
148- document after the operation if it exists
147+ - The **pre-image**, a document that represents the version of the
148+ document before the operation, if it exists
149+ - The **post-image**, a document that represents the version of the
150+ document after the operation, if it exists
151+
152+ .. important::
153+
154+ You can enable pre- and post-images on collections only if your
155+ deployment uses MongoDB v6.0 or later.
149156
150157To receive change stream events that include a pre-image or post-image, you
151- must connect to a MongoDB v6.0 or later deployment and set up the following:
158+ must perform the following actions :
152159
153160- Enable pre-images and post-images for the collection on your MongoDB
154161 deployment.
155162
156163 .. tip::
157164
158- To learn how to enable these on your deployment, see the
165+ To learn how to enable pre- and post-images on your deployment, see
159166 :manual:`Change Streams with Document Pre- and Post-Images </changeStreams/#change-streams-with-document-pre--and-post-images>`
160- {+mdb-server+} manual page .
167+ in the Server manual .
161168
162169 To learn how to instruct the driver to create a collection with pre-images
163170 and post-images enabled, see the :ref:`<java-change-stream-pre-post-collection>`
@@ -168,19 +175,19 @@ must connect to a MongoDB v6.0 or later deployment and set up the following:
168175
169176 .. tip::
170177
171- To configure your change stream to include the pre-image, see
172- the :ref:`<java-pre-image-example>`.
178+ To configure your change stream to record the pre-image in change
179+ events, see the :ref:`<java-pre-image-example>`.
173180
174- To configure your change stream to include the post-image, see the
175- :ref:`<java-post-image-example>`.
181+ To configure your change stream to record the post-image in change
182+ events, see the :ref:`<java-post-image-example>`.
176183
177184.. _java-change-stream-pre-post-collection:
178185
179186Create a Collection with Pre-Image and Post-Images Enabled
180187~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181188
182- To create a collection with the pre-image and post-image option using the
183- driver , specify an instance of ``ChangeStreamPreAndPostImagesOptions``
189+ To use the driver to create a collection with the pre-image and post-image options
190+ enabled , specify an instance of ``ChangeStreamPreAndPostImagesOptions``
184191and call the ``createCollection()`` method as shown in the following example:
185192
186193.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
@@ -191,22 +198,23 @@ and call the ``createCollection()`` method as shown in the following example:
191198
192199You can change the pre-image and post-image option in an existing collection
193200by running the ``collMod`` command from the MongoDB Shell. To learn how to
194- perform this operation, see the :manual:`collMod </reference/command/collMod#change-streams-with-document-pre--and-post-images >`
195- {+mdb-server+} manual.
201+ perform this operation, see the entry on :manual:`collMod </reference/command/collMod/ >`
202+ in the Server manual.
196203
197204.. warning::
198205
199- When you modify this option on a collection, any change streams open on
200- that collection in your application may fail if configured to require
201- receiving the pre-image or post-image .
206+ If you enabled pre-images or post-images on a collection, modifying
207+ these settings with ``collMod`` can cause existing change streams on
208+ that collection to fail .
202209
203210.. _java-pre-image-example:
204211
205212Pre-image Configuration Example
206213~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207214
208- The following code example shows how you can configure a change stream to
209- include the pre-image and output the results:
215+ The following code example shows how you can configure a change stream
216+ on the ``myColl`` collection to include the pre-image and output any
217+ change events:
210218
211219.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
212220 :language: java
@@ -215,23 +223,26 @@ include the pre-image and output the results:
215223 :end-before: end fullDocumentBeforeChangeExample
216224
217225The preceding example configures the change stream to use the
218- ``FullDocumentBeforeChange.REQUIRED`` option. This configures the change
219- stream to return pre-images for replace, update, and delete change events and
220- for the MongoDB deployment to raise an error if the pre-image is unavailable .
226+ ``FullDocumentBeforeChange.REQUIRED`` option. This option configures the change
227+ stream to require pre-images for replace, update, and delete change
228+ events. If the pre-image is not available, the driver raises an error .
221229
222- Suppose an application updated the ``latestVersion`` field of a document in a
223- collection of software library dependencies from the value of ``2.0.0`` to
224- ``2.1.0``. The corresponding change event output by the preceding code example
225- should resemble the following text:
230+ Suppose you update the value of the ``amount`` field in a document from
231+ ``150`` to ``2000``. This change event produces the following output:
226232
227- .. code-block:: text
228- :emphasize-lines: 3
233+ .. code-block::
234+ :emphasize-lines: 7
229235 :copyable: false
230236
231- Received a change: ChangeStreamDocument{ operationType=update, resumeToken={...}
232- namespace=software.libraries, destinationNamespace=null, fullDocument=null,
233- fullDocumentBeforeChange=Document{{_id=6388..., latestVersion=2.0.0, ...}},
234- ...
237+ Received a change: ChangeStreamDocument{
238+ operationType=update,
239+ resumeToken={"_data": "..."},
240+ namespace=myDb.myColl,
241+ destinationNamespace=null,
242+ fullDocument=null,
243+ fullDocumentBeforeChange=Document{{_id=..., amount=150, ...}},
244+ ...
245+ }
235246
236247For a list of options, see the `FullDocumentBeforeChange <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/changestream/FullDocumentBeforeChange.html>`__
237248API documentation.
@@ -241,8 +252,9 @@ API documentation.
241252Post-image Configuration Example
242253~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243254
244- The following code example shows how you can configure a change stream to
245- include the post-image and output the results:
255+ The following code example shows how you can configure a change stream
256+ on the ``myColl`` collection to include the pre-image and output any
257+ change events:
246258
247259.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
248260 :language: java
@@ -251,25 +263,28 @@ include the post-image and output the results:
251263 :end-before: end fullDocumentExample
252264
253265The preceding example configures the change stream to use the
254- ``FullDocument.UPDATE_LOOKUP`` option. This configures the change
255- stream to return both the deltas between the original and changed document
256- and a copy of the document at some point in time after the change occurred.
266+ ``FullDocument.WHEN_AVAILABLE`` option. This option configures the change
267+ stream to return the post-image of the modified document for replace and
268+ update change events, if it's available.
269+
270+ Suppose you update the value of the ``color`` field in a document from
271+ ``"purple"`` to ``"pink"``. The change event produces the following
272+ output:
257273
258- Suppose an application updated the ``population`` field of a document from
259- the value of ``800`` to ``950`` in a collection of city census data. The
260- corresponding change event output by the preceding code example should
261- resemble the following text:
262274
263- .. code-block:: text
264- :emphasize-lines: 3
275+ .. code-block::
276+ :emphasize-lines: 6-7
265277 :copyable: false
266278
267- Received a change: ChangeStreamDocument{ operationType=update, resumeToken={...},
268- namespace=censusData.cities, destinationNamespace=null,
269- fullDocument=Document{{_id=6388..., city=Springfield, population=950, ...}},
270- updatedFields={"population": 950}, ...
271- ...
279+ Received a change: ChangeStreamDocument{
280+ operationType=update,
281+ resumeToken={"_data": "..."},
282+ namespace=myDb.myColl,
283+ destinationNamespace=null,
284+ fullDocument=Document{{_id=..., color=purple, ...}},
285+ updatedFields={"color": purple},
286+ ...
287+ }
272288
273289For a list of options, see the `FullDocument <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/changestream/FullDocument.html>`__
274290API documentation.
275-
0 commit comments