4
4
Watch for Changes
5
5
=================
6
6
7
- .. default-domain:: mongodb
8
-
9
7
.. contents:: On this page
10
8
:local:
11
9
:backlinks: none
@@ -33,11 +31,7 @@ snippet:
33
31
:start-after: begin insertDocs
34
32
:end-before: end insertDocs
35
33
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
41
35
42
36
Each document contains a description of a university course that
43
37
includes the course title and maximum enrollment, corresponding to
@@ -52,7 +46,8 @@ Open a Change Stream
52
46
--------------------
53
47
54
48
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.
56
51
57
52
Example
58
53
~~~~~~~
@@ -81,7 +76,7 @@ your changes as they occur. Inserting a document with a ``title`` value
81
76
of "Advanced Screenwriting" and an ``enrollment`` value of ``20``
82
77
results in the following change-stream event:
83
78
84
- .. code-block:: go
79
+ .. code-block:: none
85
80
:copyable: false
86
81
87
82
map[_id:map[_data:...] clusterTime: {...} documentKey:map[_id:ObjectID("...")]
@@ -118,53 +113,86 @@ new delete operations:
118
113
119
114
pipeline := bson.D{{"$match", bson.D{{"operationType", "delete"}}}}
120
115
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":"..."}}}
139
116
140
117
.. note::
141
118
142
119
The ``Watch()`` method was called on the ``db`` database, so the code outputs
143
120
new delete operations in any collection within this database.
144
121
145
- Modify the Behavior of Watch()
146
- ------------------------------
122
+ Modify the Behavior of `` Watch()``
123
+ ----------------------------------
147
124
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.
149
126
150
- You can specify the following options in the opts parameter :
127
+ You can specify the following options for the ``Watch()`` method :
151
128
152
129
- ``ResumeAfter``
153
130
- ``StartAfter``
154
131
- ``FullDocument``
132
+ - ``FullDocumentBeforeChange``
155
133
- ``BatchSize``
156
134
- ``MaxAwaitTime``
157
135
- ``Collation``
158
136
- ``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.
159
184
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.
162
189
163
190
Example
164
191
~~~~~~~
165
192
166
193
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:
168
196
169
197
.. code-block:: go
170
198
@@ -185,7 +213,7 @@ Updating the ``enrollment`` value of the document with the
185
213
``title`` of "World Fiction" from ``35`` to ``30`` results in the
186
214
following change-stream event:
187
215
188
- .. code-block:: go
216
+ .. code-block:: none
189
217
:copyable: false
190
218
191
219
{"_id": {"_data": "..."},"operationType": "update","clusterTime": {"$timestamp":
@@ -196,7 +224,7 @@ following change-stream event:
196
224
"removedFields": [],"truncatedArrays": []}}
197
225
198
226
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 .
200
228
201
229
Additional Information
202
230
----------------------
0 commit comments