@@ -19,9 +19,11 @@ Overview
19
19
In this guide, you can learn how to use a **change stream** to monitor
20
20
real-time changes to your database. A change stream is a {+mdb-server+}
21
21
feature 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
23
25
operators 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
25
27
events to include the document data before and after the change.
26
28
27
29
Learn 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
78
80
:start-after: begin openChangeStreamExample
79
81
:end-before: end openChangeStreamExample
80
82
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:
83
84
84
85
.. code-block::
85
86
:copyable: false
86
87
87
88
Received a change: ChangeStreamDocument{
88
- operationType=' insert' ,
89
- resumeToken={"_data": "825EC ..."},
89
+ operationType=insert,
90
+ resumeToken={"_data": "..."},
90
91
namespace=myDb.myColl,
91
92
...
92
93
}
@@ -111,7 +112,6 @@ to specify which change events the change stream receives.
111
112
To learn which aggregation operators your {+mdb-server+} version supports, see
112
113
:ref:`<change-stream-modify-output>`.
113
114
114
-
115
115
Example
116
116
~~~~~~~
117
117
@@ -125,15 +125,17 @@ update operations:
125
125
:start-after: begin aggregationExample
126
126
:end-before: end aggregationExample
127
127
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:
130
129
131
- .. code-block:: text
130
+ .. code-block::
132
131
:copyable: false
133
132
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
+ }
137
139
138
140
.. _java-change-stream-configure-pre-post:
139
141
@@ -142,22 +144,27 @@ Include Pre-images and Post-images
142
144
143
145
You can configure the change event to contain or omit the following data:
144
146
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.
149
156
150
157
To 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 :
152
159
153
160
- Enable pre-images and post-images for the collection on your MongoDB
154
161
deployment.
155
162
156
163
.. tip::
157
164
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
159
166
: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 .
161
168
162
169
To learn how to instruct the driver to create a collection with pre-images
163
170
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:
168
175
169
176
.. tip::
170
177
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>`.
173
180
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>`.
176
183
177
184
.. _java-change-stream-pre-post-collection:
178
185
179
186
Create a Collection with Pre-Image and Post-Images Enabled
180
187
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181
188
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``
184
191
and call the ``createCollection()`` method as shown in the following example:
185
192
186
193
.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
@@ -191,22 +198,23 @@ and call the ``createCollection()`` method as shown in the following example:
191
198
192
199
You can change the pre-image and post-image option in an existing collection
193
200
by 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.
196
203
197
204
.. warning::
198
205
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 .
202
209
203
210
.. _java-pre-image-example:
204
211
205
212
Pre-image Configuration Example
206
213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207
214
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:
210
218
211
219
.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
212
220
:language: java
@@ -215,23 +223,26 @@ include the pre-image and output the results:
215
223
:end-before: end fullDocumentBeforeChangeExample
216
224
217
225
The 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 .
221
229
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:
226
232
227
- .. code-block:: text
228
- :emphasize-lines: 3
233
+ .. code-block::
234
+ :emphasize-lines: 7
229
235
:copyable: false
230
236
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
+ }
235
246
236
247
For a list of options, see the `FullDocumentBeforeChange <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/changestream/FullDocumentBeforeChange.html>`__
237
248
API documentation.
@@ -241,8 +252,9 @@ API documentation.
241
252
Post-image Configuration Example
242
253
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243
254
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:
246
258
247
259
.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
248
260
:language: java
@@ -251,25 +263,28 @@ include the post-image and output the results:
251
263
:end-before: end fullDocumentExample
252
264
253
265
The 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:
257
273
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:
262
274
263
- .. code-block:: text
264
- :emphasize-lines: 3
275
+ .. code-block::
276
+ :emphasize-lines: 6-7
265
277
:copyable: false
266
278
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
+ }
272
288
273
289
For a list of options, see the `FullDocument <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/changestream/FullDocument.html>`__
274
290
API documentation.
275
-
0 commit comments