Skip to content

Commit fef1ca1

Browse files
authoredFeb 10, 2023
DOCSP-22507: changestream pre and post images (mongodb#228)
* DOCSP-22507: change stream pre and post images * created new section * first pass fixes * changed output format * MW PR fixes 1 * MW suggestions
1 parent 66b78e7 commit fef1ca1

File tree

1 file changed

+64
-36
lines changed
  • source/fundamentals/crud/read-operations

1 file changed

+64
-36
lines changed
 

‎source/fundamentals/crud/read-operations/watch.txt

+64-36
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Watch for Changes
55
=================
66

7-
.. default-domain:: mongodb
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none
@@ -33,11 +31,7 @@ snippet:
3331
:start-after: begin insertDocs
3432
:end-before: end insertDocs
3533

36-
.. tip:: Non-existent Databases and Collections
37-
38-
If the necessary database and collection don't exist when
39-
you perform a write operation, the server implicitly creates
40-
them.
34+
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
4135

4236
Each document contains a description of a university course that
4337
includes the course title and maximum enrollment, corresponding to
@@ -52,7 +46,8 @@ Open a Change Stream
5246
--------------------
5347

5448
To open a change stream, use the ``Watch()`` method. The ``Watch()`` method requires a context
55-
parameter and a pipeline parameter. To return all changes, pass in an empty Pipeline object.
49+
parameter and a pipeline parameter. To return all changes, pass in an
50+
empty ``Pipeline`` object.
5651

5752
Example
5853
~~~~~~~
@@ -81,7 +76,7 @@ your changes as they occur. Inserting a document with a ``title`` value
8176
of "Advanced Screenwriting" and an ``enrollment`` value of ``20``
8277
results in the following change-stream event:
8378

84-
.. code-block:: go
79+
.. code-block:: none
8580
:copyable: false
8681

8782
map[_id:map[_data:...] clusterTime: {...} documentKey:map[_id:ObjectID("...")]
@@ -118,53 +113,86 @@ new delete operations:
118113

119114
pipeline := bson.D{{"$match", bson.D{{"operationType", "delete"}}}}
120115
changeStream, err := db.Watch(context.TODO(), mongo.Pipeline{pipeline})
121-
if err != nil {
122-
panic(err)
123-
}
124-
defer changeStream.Close(context.TODO())
125-
126-
for changeStream.Next(context.TODO()) {
127-
fmt.Println(changeStream.Current)
128-
}
129-
130-
Deleting the document with the ``title`` value of "Advanced Screenwriting"
131-
in a separate program or shell results in the following change-stream event:
132-
133-
.. code-block:: go
134-
:copyable: false
135-
136-
{"_id": {"_data": "..."},"operationType": "delete","clusterTime":
137-
{"$timestamp":{"t":"...","i":"..."}},"ns": {"db": "db","coll": "courses"},
138-
"documentKey": {"_id": {"$oid":"..."}}}
139116

140117
.. note::
141118

142119
The ``Watch()`` method was called on the ``db`` database, so the code outputs
143120
new delete operations in any collection within this database.
144121

145-
Modify the Behavior of Watch()
146-
------------------------------
122+
Modify the Behavior of ``Watch()``
123+
----------------------------------
147124

148-
Use an opts parameter to modify the behavior of the ``Watch()`` method.
125+
Use the ``options`` parameter to modify the behavior of the ``Watch()`` method.
149126

150-
You can specify the following options in the opts parameter:
127+
You can specify the following options for the ``Watch()`` method:
151128

152129
- ``ResumeAfter``
153130
- ``StartAfter``
154131
- ``FullDocument``
132+
- ``FullDocumentBeforeChange``
155133
- ``BatchSize``
156134
- ``MaxAwaitTime``
157135
- ``Collation``
158136
- ``StartAtOperationTime``
137+
- ``Comment``
138+
- ``ShowExpandedEvents``
139+
- ``StartAtOperationTime``
140+
- ``Custom``
141+
- ``CustomPipeline``
142+
143+
For more information on these options, visit the
144+
:manual:`MongoDB Server manual </reference/method/db.collection.watch/>`.
145+
146+
Pre- and Post-Images
147+
~~~~~~~~~~~~~~~~~~~~
148+
149+
When you perform any CRUD operation on a collection, by default, the
150+
corresponding change event document contains only the delta of the fields modified
151+
by the operation. You can see the full document before and after a
152+
change, in addition to the delta, by specifying settings in the ``options``
153+
parameter of the ``Watch()`` method.
154+
155+
If you want to see a document's post-image, the full version of the
156+
document after a change, set the ``FullDocument`` field of the
157+
``options`` parameter to one of the following values:
158+
159+
- ``UpdateLookup``: The change event document includes a copy of the
160+
entire changed document.
161+
- ``WhenAvailable``: The change event document includes a post-image of
162+
the modified document for change events if the
163+
post-image is available.
164+
- ``Required``: The output is the same as for ``WhenAvailable``, but the
165+
driver raises a server-side error if the post-image is not available.
166+
167+
If you want to see a document's pre-image, the full version of the
168+
document before a change, set the ``FullDocumentBeforeChange`` field of the
169+
``options`` parameter to one of the following values:
170+
171+
- ``WhenAvailable``: The change event document includes a pre-image of
172+
the modified document for change events if the
173+
pre-image is available.
174+
- ``Required``: The output is the same as for ``WhenAvailable``, but the
175+
driver raises a server-side error if the pre-image is not available.
176+
177+
.. important::
178+
179+
To access document pre- and post-images, you must enable
180+
``changeStreamPreAndPostImages`` for the collection. See the
181+
:manual:`MongoDB Server manual
182+
</reference/command/collMod/#change-streams-with-document-pre--and-post-images>` for instructions and more
183+
information.
159184

160-
For more information on these fields, visit the
161-
:manual:`MongoDB manual </reference/method/Mongo.watch/#mongodb-method-Mongo.watch>`.
185+
.. note::
186+
187+
There is no pre-image for an inserted document and no post-image for
188+
a deleted document.
162189

163190
Example
164191
~~~~~~~
165192

166193
The following example calls the ``Watch()`` method on the ``db.courses`` collection. It
167-
specifies the ``FullDocument`` options parameter to output a copy of the entire modified document:
194+
specifies a value for the ``FullDocument`` field of the ``options`` parameter to
195+
output a copy of the entire modified document, instead of only the changed fields:
168196

169197
.. code-block:: go
170198

@@ -185,7 +213,7 @@ Updating the ``enrollment`` value of the document with the
185213
``title`` of "World Fiction" from ``35`` to ``30`` results in the
186214
following change-stream event:
187215

188-
.. code-block:: go
216+
.. code-block:: none
189217
:copyable: false
190218

191219
{"_id": {"_data": "..."},"operationType": "update","clusterTime": {"$timestamp":
@@ -196,7 +224,7 @@ following change-stream event:
196224
"removedFields": [],"truncatedArrays": []}}
197225

198226
Without specifying the ``FullDocument`` option, the same update operation no longer
199-
outputs the ``"fullDocument"`` value.
227+
outputs the ``"fullDocument"`` value in the change event document.
200228

201229
Additional Information
202230
----------------------

0 commit comments

Comments
 (0)
Please sign in to comment.