@@ -12,7 +12,7 @@ Change Stream Examples
12
12
13
13
.. important::
14
14
Change streams are available for replica sets or sharded clusters with replica set
15
- shards. You cannot open a change stream against a standalone :program:`mongod`.
15
+ shards. You cannot open a change stream against a standalone :program:`mongod`.
16
16
For a sharded cluster, you must issue the open change stream operation against
17
17
the :program:`mongos`.
18
18
@@ -22,9 +22,6 @@ Change Stream Examples
22
22
23
23
- :ref:`WiredTiger <storage-wiredtiger>` storage engine (can be :ref:`encrypted <encrypted-storage-engine>`)
24
24
25
-
26
-
27
-
28
25
.. contents:: On this page
29
26
:local:
30
27
:backlinks: none
@@ -36,57 +33,53 @@ Change Stream Examples
36
33
tabs:
37
34
- id: python
38
35
content: |
39
-
36
+
40
37
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
41
38
<http://api.mongodb.com/python/current/tutorial.html?_ga=2.166792574.1325858930.1510605946-1663796716.1426889906#making-a-connection-with-mongoclient/>`_
42
39
that contains an ``inventory`` collection.
43
40
44
-
45
41
- id: java-sync
46
42
content: |
47
43
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
48
44
<http://mongodb.github.io/mongo-java-driver/3.6/driver/tutorials/databases-collections/>`_
49
45
that contains an ``inventory`` collection.
50
-
46
+
51
47
- id: csharp
52
48
content: |
53
49
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
54
- <http://mongodb.github.io/mongo-csharp-driver/2.4/getting_started/quick_tour/#make-a-connection/>`_
50
+ <http://mongodb.github.io/mongo-csharp-driver/2.4/getting_started/quick_tour/#make-a-connection/>`_
55
51
that contains an ``inventory`` collection.
56
-
52
+
57
53
- id: c
58
54
content: |
59
55
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
60
56
<http://mongoc.org/libmongoc/current/tutorial.html#making-a-connection/>`_
61
57
that contains an ``inventory`` collection.
62
-
63
-
64
58
65
59
Open A Change Stream
66
60
--------------------
67
-
61
+
68
62
This example opens a change stream against a replica set. The change stream is bound to a collection and
69
63
change stream documents are iterated with a cursor. This cursor remains open until it is explicitly closed,
70
64
as long as a connection to the MongoDB deployment remains open *and* the collection exists.
71
65
72
-
73
66
.. tabs-drivers::
74
67
75
68
tabs:
76
69
- id: python
77
70
content: |
78
71
.. class:: copyable-code
79
-
72
+
80
73
.. literalinclude:: /driver-examples/test_examples.py
81
74
:language: python
82
75
:dedent: 8
83
- :start-after: Start Changestream Example 1
76
+ :start-after: Start Changestream Example 1
84
77
:end-before: End Changestream Example 1
85
78
86
79
- id: java-sync
87
80
content: |
88
81
.. class:: copyable-code
89
-
82
+
90
83
.. literalinclude:: /driver-examples/DocumentationSamples.java
91
84
:language: java
92
85
:dedent: 8
@@ -101,11 +94,11 @@ as long as a connection to the MongoDB deployment remains open *and* the collect
101
94
:dedent: 12
102
95
:start-after: Start Changestream Example 1
103
96
:end-before: End Changestream Example 1
104
-
97
+
105
98
- id: c
106
99
content: |
107
100
.. class:: copyable-code
108
-
101
+
109
102
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
110
103
:language: c
111
104
:dedent: 0
@@ -129,44 +122,44 @@ Lookup Full Document for Update Operations
129
122
tabs:
130
123
- id: python
131
124
content: |
132
- By default, change streams only return the delta of fields during the update operation.
125
+ By default, change streams only return the delta of fields during the update operation.
133
126
To return the most current majority-committed version of the updated
134
127
document, pass ``full_document='updateLookup'`` to the
135
- :method:`db.collection.watch()` method.
136
-
128
+ :method:`db.collection.watch()` method.
129
+
137
130
- id: java-sync
138
131
content: |
139
- By default, change streams only return the delta of fields during the update operation.
132
+ By default, change streams only return the delta of fields during the update operation.
140
133
To return the most current majority-committed version of the updated
141
134
document, pass ``FullDocument.UPDATE_LOOKUP`` to the
142
- :method:`db.collection.watch.fullDocument()` method.
143
-
135
+ :method:`db.collection.watch.fullDocument()` method.
136
+
144
137
- id: c
145
138
content: |
146
- By default, change streams only return the delta of fields during the update operation.
139
+ By default, change streams only return the delta of fields during the update operation.
147
140
To return the most current majority-committed version of the updated
148
141
document, pass the ``"fullDocument"`` option with the ``"updateLookup"`` value to the
149
- :method:`mongoc_collection_watch` method.
150
-
142
+ :method:`mongoc_collection_watch` method.
143
+
151
144
- id: csharp
152
145
content: |
153
- By default, change streams only return the delta of fields during the update operation.
146
+ By default, change streams only return the delta of fields during the update operation.
154
147
To return the most current majority-committed version of the updated
155
148
document, pass ``"FullDocument = ChangeStreamFullDocumentOption.UpdateLookup"`` to the
156
- :method:`collection.Watch()` method.
149
+ :method:`collection.Watch()` method.
157
150
158
151
159
152
If there are one or more majority-committed operations that modified the
160
153
updated document *after* the update operation but *before* the lookup, the
161
154
full document returned may differ significantly from the document at the time
162
155
of the update operation.
163
-
164
- However, the deltas included in the change stream document always
165
- correctly describe the watched
156
+
157
+ However, the deltas included in the change stream document always
158
+ correctly describe the watched
166
159
collection changes that applied to that change stream event.
167
160
168
161
Iterate the change stream ``cursor`` to retrieve the change stream
169
- documents in order. In the example below, all update operations notifications
162
+ documents in order. In the example below, all update operations notifications
170
163
include a ``fullDocument``
171
164
field that represents the *current* version of the document affected by the
172
165
update operation.
@@ -177,17 +170,17 @@ update operation.
177
170
- id: python
178
171
content: |
179
172
.. class:: copyable-code
180
-
173
+
181
174
.. literalinclude:: /driver-examples/test_examples.py
182
175
:language: python
183
176
:dedent: 8
184
- :start-after: Start Changestream Example 2
177
+ :start-after: Start Changestream Example 2
185
178
:end-before: End Changestream Example 2
186
179
187
180
- id: java-sync
188
181
content: |
189
182
.. class:: copyable-code
190
-
183
+
191
184
.. literalinclude:: /driver-examples/DocumentationSamples.java
192
185
:language: java
193
186
:dedent: 8
@@ -197,17 +190,17 @@ update operation.
197
190
- id: csharp
198
191
content: |
199
192
.. class:: copyable-code
200
-
193
+
201
194
.. literalinclude:: /driver-examples/ChangeStreamExamples.cs
202
195
:language: C#
203
196
:dedent: 12
204
197
:start-after: Start Changestream Example 2
205
198
:end-before: End Changestream Example 2
206
-
199
+
207
200
- id: c
208
201
content: |
209
202
.. class:: copyable-code
210
-
203
+
211
204
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
212
205
:language: C
213
206
:dedent: 0
@@ -254,7 +247,6 @@ more of the following pipeline stages when configuring the change stream:
254
247
255
248
MongoCursor<Document> cursor = collection.watch(pipeline).iterator();
256
249
257
-
258
250
.. tabs-drivers::
259
251
260
252
tabs:
@@ -271,8 +263,7 @@ more of the following pipeline stages when configuring the change stream:
271
263
See :ref:`change-stream-output` for more information on the change stream
272
264
response document format.
273
265
274
-
275
- .. change-stream-resume::
266
+ .. _change-stream-resume:
276
267
277
268
Resume a Change Stream
278
269
----------------------
@@ -291,13 +282,13 @@ notification after a specific notification.
291
282
token. Passing the ``resumeToken`` to the ``resumeAfter`` modifier directs
292
283
the change stream to attempt to resume notifications starting at the
293
284
operation specified in the resume token.
294
-
285
+
295
286
.. class:: copyable-code
296
-
287
+
297
288
.. literalinclude:: /driver-examples/test_examples.py
298
289
:language: python
299
290
:dedent: 8
300
- :start-after: Start Changestream Example 3
291
+ :start-after: Start Changestream Example 3
301
292
:end-before: End Changestream Example 3
302
293
303
294
- id: java-sync
@@ -306,9 +297,9 @@ notification after a specific notification.
306
297
token. Passing the ``resumeToken`` to the ``resumeAfter()`` method directs
307
298
the change stream to attempt to resume notifications starting at the
308
299
operation specified in the resume token.
309
-
300
+
310
301
.. class:: copyable-code
311
-
302
+
312
303
.. literalinclude:: /driver-examples/DocumentationSamples.java
313
304
:language: java
314
305
:dedent: 8
@@ -318,37 +309,35 @@ notification after a specific notification.
318
309
- id: csharp
319
310
content: |
320
311
In the example below, the ``resumeToken`` is retrieved from the last change stream document
321
- and passed to the ``Watch()`` method as an option. Passing the ``resumeToken``
312
+ and passed to the ``Watch()`` method as an option. Passing the ``resumeToken``
322
313
to the ``Watch()`` method directs
323
314
the change stream to attempt to resume notifications starting at the
324
315
operation specified in the resume token.
325
-
316
+
326
317
.. class:: copyable-code
327
-
318
+
328
319
.. literalinclude:: /driver-examples/ChangeStreamExamples.cs
329
320
:language: C#
330
321
:dedent: 12
331
322
:start-after: Start Changestream Example 3
332
323
:end-before: End Changestream Example 3
333
-
324
+
334
325
- id: c
335
326
content: |
336
327
In the example below, the ``resumeAfter`` option is appended to the stream options
337
328
to recreate the stream after it has been destroyed. Passing the ``_id` to
338
329
the change stream attempts to resume notifications starting at the
339
330
operation specified.
340
-
331
+
341
332
.. class:: copyable-code
342
-
333
+
343
334
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
344
335
:language: C
345
336
:dedent: 0
346
337
:start-after: Start Changestream Example 3
347
338
:end-before: End Changestream Example 3
348
339
349
340
350
-
351
-
352
341
As long as that operation has not rolled off the :term:`oplog`, the
353
342
change stream can successfully resume notifications.
354
343
0 commit comments