-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathchangeStreams.txt
1291 lines (939 loc) · 42.7 KB
/
changeStreams.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.. facet::
:name: programming_language
:values: c, csharp, go, java, javascript/typescript, kotlin, php, python, ruby, swift
.. _changeStreams:
.. _collection_watch:
==============
Change Streams
==============
.. default-domain:: mongodb
.. facet::
:name: genre
:values: reference
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: twocols
.. meta::
:description: Change streams code examples for how to access real-time data changes in MongoDB
:keywords: database triggers, code example, node.js, motor, coroutine, java sync, swift sync, swift async, compass
Change streams allow applications to access real-time data changes
without the prior complexity and risk of manually tailing the :term:`oplog`.
Applications can use change streams to subscribe to all data changes on
a single collection, a database, or an entire deployment, and
immediately react to them. Because change streams use the aggregation
framework, applications can also filter for specific changes or
transform the notifications at will.
.. |change-streams| replace:: change streams
.. include:: /includes/change-streams-optimization.rst
Availability
------------
Change streams are available for :ref:`replica sets <replication>` and
:ref:`sharded clusters <sharding-background>`:
- **Storage Engine.**
The replica sets and sharded clusters must use the :ref:`WiredTiger
<storage-wiredtiger>` storage engine. Change streams can also be used
on deployments that employ MongoDB's
:ref:`encryption-at-rest<encrypted-storage-engine>` feature.
- **Replica Set Protocol Version.**
The replica sets and sharded clusters must use replica set protocol
version 1 (:rsconf:`pv1 <protocolVersion>`).
- **Read Concern "majority" Enablement.**
.. include:: /includes/extracts/changestream-rc-majority-4.2.rst
Stable API Support
~~~~~~~~~~~~~~~~~~
.. include:: /includes/stable-api/change-stream-support.rst
Connect
--------
Connections for a change stream can either use DNS seed lists
with the ``+srv`` connection option or by listing the servers individually
in the connection string.
If the driver loses the connection to a change stream or the connection goes
down, it attempts to reestablish a connection to the change stream through
another node in the cluster that has a matching
:ref:`read preference <connections-read-preference>`. If the driver cannot find
a node with the correct read preference, it throws an exception.
For more information, see :ref:`Connection String URI Format <mongodb-uri>`.
.. _changeStreams-watch-deployment:
Watch a Collection, Database, or Deployment
-------------------------------------------
You can open change streams against:
.. list-table::
:header-rows: 1
:widths: 15 85
* - Target
- Description
* - A collection
- You can open a change stream cursor for a single collection
(except ``system`` collections, or any collections in the
``admin``, ``local``, and ``config`` databases).
The examples on this page use the MongoDB drivers to open and
work with a change stream cursor for a single collection. See
also the :binary:`~bin.mongosh` method
:method:`db.collection.watch()`.
* - A database
- You can open a change stream cursor for a single database (excluding
``admin``, ``local``, and ``config`` database) to watch for changes to
all its non-system collections.
For the MongoDB driver method, refer to your driver
documentation. See also the :binary:`~bin.mongosh` method
:method:`db.watch()`.
* - A deployment
- You can open a change stream cursor for a deployment (either a replica
set or a sharded cluster) to watch for changes to all non-system
collections across all databases except for ``admin``, ``local``, and
``config``.
For the MongoDB driver method, refer to your driver
documentation. See also the :binary:`~bin.mongosh` method
:method:`Mongo.watch()`.
.. note:: Change Stream Examples
The examples on this page use the MongoDB drivers to illustrate how
to open a change stream cursor for a collection and work with the
change stream cursor.
Change Stream Performance Considerations
----------------------------------------
If the amount of active change streams opened against a database exceeds the
:ref:`connection pool size <connection-pool-overview>`, you may
experience notification latency. Each change stream uses a connection
and a :ref:`getMore <manual-reference-commands-getMore>`
operation on the change stream for the period of time that it waits for the next event.
To avoid any latency issues, you should ensure that the pool size is greater than the
number of opened change streams. For details see the :ref:`maxPoolSize<maxpoolsize-cp-setting>` setting.
Sharded Cluster Considerations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When a change stream is opened on a sharded cluster:
- The :binary:`mongos` creates individual change streams on **each
shard**. This behavior occurs regardless of whether the change stream
targets a particular shard key range.
- When the ``mongos`` receives change stream results, it sorts and
filters those results. If needed, the ``mongos`` also performs a
``fullDocument`` lookup.
For best performance, limit the use of :pipeline:`$lookup` queries in
change streams.
.. _open-change-stream:
Open A Change Stream
--------------------
To open a change stream:
- For a replica set, you can issue the open change stream operation
from any of the data-bearing members.
- For a sharded cluster, you must issue the open change stream
operation from the :binary:`~bin.mongos`.
The following example opens a change stream for a collection and
iterates over the cursor to retrieve the change stream documents.
[#start-time]_
----------
.. |arrow| unicode:: U+27A4
|arrow| Use the **Select your language** drop-down menu in the
upper-right to set the language of the examples on this page.
----------
.. tabs-selector:: drivers
.. tabs-drivers::
.. tab::
:tabid: python
The Python examples below assume that you have :driver:`connected to a MongoDB replica set and have accessed a database
</pymongo>` that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/test_examples.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: motor
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<https://motor.readthedocs.io/en/stable/tutorial-asyncio.html#creating-a-client>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/test_examples_motor.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: java-sync
The Java examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<http://mongodb.github.io/mongo-java-driver/3.6/driver/tutorials/databases-collections/>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/DocumentationSamples.java
:language: java
:dedent: 8
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: kotlin-coroutine
The Kotlin examples below assume that you are connected to a MongoDB replica set and can access a database
that contains the ``inventory`` collection. To learn more about completing these tasks, see the
:driver:`Kotlin Coroutine Driver Databases and Collections </kotlin/coroutine/current/fundamentals/databases-collections/>` guide.
.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent:
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: csharp
The C# examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<http://mongodb.github.io/mongo-csharp-driver/2.4/getting_started/quick_tour/#make-a-connection/>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/ChangeStreamExamples.cs
:language: csharp
:dedent: 12
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: c
The C examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<https://www.mongodb.com/docs/languages/c/c-driver/current/databases-collections/#access-a-database>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
:language: c
:dedent: 3
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: nodejs
The Node.js examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#connect>`__
that contains an ``inventory`` collection.
The following example uses stream to process the change events.
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
Alternatively, you can also use iterator to process the change events:
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 1 Alternative
:end-before: End Changestream Example 1 Alternative
ChangeStream extends `EventEmitter
<https://mongodb.github.io/node-mongodb-native/5.7/classes/TypedEventEmitter.html>`__.
.. tab::
:tabid: php
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<https://www.mongodb.com/docs/php-library/current/reference/method/MongoDBClient__construct/>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/DocumentationExamplesTest.php
:language: php
:dedent: 8
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: ruby
The examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<https://www.mongodb.com/docs/ruby-driver/current/reference/create-client/>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/change_stream_examples_spec.rb
:language: ruby
:dedent: 8
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: go
The Go examples below assume that you have `connected to a MongoDB replica set and have accessed a database
<https://godoc.org/go.mongodb.org/mongo-driver/mongo#NewClient/>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/go_examples.go
:language: go
:dedent: 2
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: swift-sync
The Swift (Sync) examples below assume that you have
`connected to a MongoDB replica set and have accessed a
database
<https://mongodb.github.io/mongo-swift-driver/docs/current/MongoSwiftSync/Classes/MongoClient.html>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/swiftSync.swift
:language: swift
:dedent: 8
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
.. tab::
:tabid: swift-async
The Swift (Async) examples below assume that you have
`connected to a MongoDB replica set and have accessed a
database
<https://mongodb.github.io/mongo-swift-driver/docs/current/MongoSwift/Classes/MongoClient.html>`__
that contains an ``inventory`` collection.
.. literalinclude:: /driver-examples/swiftAsync.swift
:language: swift
:dedent: 8
:start-after: Start Changestream Example 1
:end-before: End Changestream Example 1
To retrieve the :ref:`data change event <change-stream-output>` from
the cursor, iterate the change stream cursor. For information on the
change stream event, see :ref:`change-stream-output`.
.. include:: /includes/extracts/changestream-cursor-open.rst
.. note::
The lifecycle of an unclosed cursor is language-dependent.
.. [#start-time]
You can specify a ``startAtOperationTime`` to open the cursor at a particular
point in time. If the specified starting point is in the past, it must be in
the time range of the oplog.
.. _change-stream-modify-output:
Modify Change Stream Output
---------------------------
----------
|arrow| Use the **Select your language** drop-down menu in the
upper-right to set the language of the examples on this page.
----------
.. tabs-drivers::
.. tab::
:tabid: python
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/test_examples.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: motor
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/test_examples_motor.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: java-sync
.. include:: /includes/fact-change-streams-modify-output.rst
.. code-block:: java
MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<host>:<port>");
// Select the MongoDB database and collection to open the change stream against
MongoDatabase db = mongoClient.getDatabase("myTargetDatabase");
MongoCollection<Document> collection = db.getCollection("myTargetCollection");
// Create $match pipeline stage.
List<Bson> pipeline = singletonList(Aggregates.match(Filters.or(
Document.parse("{'fullDocument.username': 'alice'}"),
Filters.in("operationType", asList("delete")))));
// Create the change stream cursor, passing the pipeline to the
// collection.watch() method
MongoCursor<Document> cursor = collection.watch(pipeline).iterator();
The ``pipeline`` list includes a single :pipeline:`$match` stage that
filters for any operations that meet one or both of the following criteria:
- ``username`` value is ``alice``
- ``operationType`` value is ``delete``
Passing the ``pipeline`` to the :method:`~db.collection.watch()` method directs the
change stream to return notifications after passing them through the
specified ``pipeline``.
.. tab::
:tabid: kotlin-coroutine
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent:
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
The ``pipeline`` list includes a single :pipeline:`$match` stage that
filters for any operations that meet one or both of the following criteria:
- ``username`` value is ``alice``
- ``operationType`` value is ``delete``
Passing the ``pipeline`` to the :method:`~db.collection.watch()` method directs the
change stream to return notifications after passing them through the
specified ``pipeline``.
.. tab::
:tabid: csharp
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/ChangeStreamExamples.cs
:language: csharp
:dedent: 16
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: c
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
:language: c
:dedent: 3
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: nodejs
.. include:: /includes/fact-change-streams-modify-output.rst
The following example uses stream to process the change events.
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
Alternatively, you can also use iterator to process the change events:
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 4 Alternative
:end-before: End Changestream Example 4 Alternative
.. tab::
:tabid: php
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/DocumentationExamplesTest.php
:language: php
:dedent: 8
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: ruby
.. include:: /includes/fact-change-streams-modify-output.rst
.. tab::
:tabid: go
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/go_examples.go
:language: go
:dedent: 2
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: swift-sync
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/swiftSync.swift
:language: swift
:dedent: 8
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tab::
:tabid: swift-async
.. include:: /includes/fact-change-streams-modify-output.rst
.. literalinclude:: /driver-examples/swiftAsync.swift
:language: swift
:dedent: 8
:start-after: Start Changestream Example 4
:end-before: End Changestream Example 4
.. tip::
The :ref:`_id <change-stream-event-id>` field of the change stream
event document act as the :ref:`resume token
<change-stream-resume>`. Do not use the pipeline to modify or remove
the change stream event's ``_id`` field.
.. include:: /includes/extracts/4.2-changes-change-stream-modification-error.rst
See :ref:`change-stream-output` for more information on the change stream
response document format.
.. _change-streams-updateLookup:
Lookup Full Document for Update Operations
------------------------------------------
By default, change streams only return the delta of fields during
the update operation. However, you can configure the change stream
to return the most current majority-committed version of the updated
document.
----------
|arrow| Use the **Select your language** drop-down menu in the
upper-right to set the language of the examples on this page.
----------
.. tabs-drivers::
.. tab::
:tabid: python
To return the most current majority-committed version of the updated
document, pass ``full_document='updateLookup'`` to the
:method:`db.collection.watch()` method.
In the example below, all update operations notifications
include a ``full_document`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/test_examples.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: motor
To return the most current majority-committed version of the updated
document, pass ``full_document='updateLookup'`` to the
:method:`db.collection.watch()` method.
In the example below, all update operations notifications
include a ```full_document`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/test_examples_motor.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: java-sync
To return the most current majority-committed version of the updated
document, pass ``FullDocument.UPDATE_LOOKUP`` to the
``db.collection.watch.fullDocument()`` method.
In the example below, all update operations notifications
include a ``FullDocument`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/DocumentationSamples.java
:language: java
:dedent: 8
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: kotlin-coroutine
To return the most current majority-committed version of the updated
document, pass ``FullDocument.UPDATE_LOOKUP`` to the
`ChangeStreamFlow.fullDocument() <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-change-stream-flow/full-document.html>`__ method.
In the example below, all update operations notifications
include a ``FullDocument`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent:
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: c
To return the most current majority-committed version of the updated
document, pass the ``"fullDocument"`` option with the ``"updateLookup"`` value to the
``mongoc_collection_watch`` method.
In the example below, all update operations notifications
include a ``fullDocument`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
:language: c
:dedent: 3
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: csharp
To return the most current majority-committed version of the updated
document, pass ``"FullDocument = ChangeStreamFullDocumentOption.UpdateLookup"`` to the
:method:`db.collection.watch()` method.
In the example below, all update operations notifications
include a ``FullDocument`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/ChangeStreamExamples.cs
:language: csharp
:dedent: 12
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: nodejs
To return the most current majority-committed version of the updated
document, pass ``{ fullDocument: 'updateLookup' }`` to the
:method:`db.collection.watch()` method.
In the example below, all update operations notifications
include a ``fullDocument`` field that represents the *current*
version of the document affected by the update operation.
The following example uses stream to process the change events.
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
Alternatively, you can also use iterator to process the change events:
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 2 Alternative
:end-before: End Changestream Example 2 Alternative
.. tab::
:tabid: php
To return the most current
majority-committed version of the updated document, pass
``"fullDocument' => \MongoDB\Operation\ChangeStreamCommand::FULL_DOCUMENT_UPDATE_LOOKUP"``
to the :method:`db.watch()` method.
In the example below, all update operations notifications
include a ``fullDocument`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/DocumentationExamplesTest.php
:language: php
:dedent: 8
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: ruby
To return the most current majority-committed version of the updated
document, pass ``full_document: 'updateLookup'`` to the
:method:`db.watch()` method.
In the example below, all update operations notifications
include a ``full_document`` field that represents the *current*
version of the document affected by the update operation.
.. literalinclude:: /driver-examples/change_stream_examples_spec.rb
:language: ruby
:dedent: 8
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: go
To return the most current majority-committed version of the
updated document, ``SetFullDocument(options.UpdateLookup)``
change stream option.
.. literalinclude:: /driver-examples/go_examples.go
:language: go
:dedent: 2
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: swift-sync
To return the most current majority-committed version of
the updated document, pass ``options:
ChangeStreamOptions(fullDocument: .updateLookup)`` to the
``watch()`` method.
.. literalinclude:: /driver-examples/swiftSync.swift
:language: swift
:dedent: 8
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. tab::
:tabid: swift-async
To return the most current majority-committed version of
the updated document, pass ``options:
ChangeStreamOptions(fullDocument: .updateLookup)`` to the
``watch()`` method.
.. literalinclude:: /driver-examples/swiftAsync.swift
:language: swift
:dedent: 8
:start-after: Start Changestream Example 2
:end-before: End Changestream Example 2
.. note::
If there are one or more majority-committed operations that modified
the updated document *after* the update operation but *before* the
lookup, the full document returned may differ significantly from the
document at the time of the update operation.
However, the deltas included in the change stream document always
correctly describe the watched collection changes that applied to
that change stream event.
The ``fullDocument`` field for an update event may be missing if one
of the following is true:
- If the document is deleted or if the collection is dropped in
between the update and the lookup.
- If the update changes the values for at least one of the fields in
that collection's shard key.
See :ref:`change-stream-output` for more information on the change
stream response document format.
.. _change-stream-resume:
Resume a Change Stream
----------------------
Change streams are resumable by specifying a resume token to either
:ref:`resumeAfter <change-stream-resume-after>` or
:ref:`startAfter <change-stream-start-after>` when opening the cursor.
.. _change-stream-resume-after:
``resumeAfter`` for Change Streams
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can resume a change stream after a specific event by passing a resume token
to ``resumeAfter`` when opening the cursor.
See :ref:`change-stream-resume-token` for more information on the resume token.
.. important::
- The oplog must have enough history to locate the operation
associated with the token or the timestamp, if the timestamp is in
the past.
- .. include:: /includes/extracts/changestream-invalid-events.rst
.. tabs-drivers::
.. tab::
:tabid: python
You can use the ``resume_after`` modifier to resume
notifications after the operation specified in the resume
token. The ``resume_after`` modifier takes a value that must
resolve to a resume token, e.g. ``resume_token`` in the
example below.
.. literalinclude:: /driver-examples/test_examples.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: motor
You can use the ``resume_after`` modifier to resume
notifications after the operation specified in the resume
token. The ``resume_after`` modifier takes a value that must
resolve to a resume token, e.g. ``resume_token`` in the
example below.
.. literalinclude:: /driver-examples/test_examples_motor.py
:language: python
:dedent: 12
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: java-sync
You can use the ``resumeAfter()`` method to resume
notifications after the operation specified in the resume
token. The ``resumeAfter()`` method takes a value that must
resolve to a resume token, e.g. ``resumeToken`` in the
example below.
.. literalinclude:: /driver-examples/DocumentationSamples.java
:language: java
:dedent: 8
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: kotlin-coroutine
You can use the `ChangeStreamFlow.resumeAfter()
<{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-change-stream-flow/resume-after.html>`__
method to resume notifications after the operation specified in the resume
token. The ``resumeAfter()`` method takes a value that must
resolve to a resume token, such as the ``resumeToken`` variable in the
example below.
.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent:
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: csharp
In the example below, the ``resumeToken`` is retrieved from the last change stream document
and passed to the ``Watch()`` method as an option. Passing the ``resumeToken``
to the ``Watch()`` method directs
the change stream to attempt to resume notifications starting after the
operation specified in the resume token.
.. literalinclude:: /driver-examples/ChangeStreamExamples.cs
:language: csharp
:dedent: 14
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: c
In the example below, the ``resumeAfter`` option is appended to the stream options
to recreate the stream after it has been destroyed. Passing the ``_id`` to
the change stream attempts to resume notifications starting after the
operation specified.
.. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
:language: C
:dedent: 3
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: nodejs
You can use the ``resumeAfter`` option to resume
notifications after the operation specified in the resume
token. The ``resumeAfter`` option takes a value that must
resolve to a resume token, e.g. ``resumeToken`` in the
example below.
.. literalinclude:: /driver-examples/node_changestreams.js
:language: javascript
:dedent: 6
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: php
You can use the ``resumeAfter`` option to resume
notifications after the operation specified in the resume
token. The ``resumeAfter`` option takes a value that must
resolve to a resume token, e.g. ``$resumeToken`` in the
example below.
.. literalinclude:: /driver-examples/DocumentationExamplesTest.php
:language: php
:dedent: 8
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: ruby
You can use the ``resume_after`` modifier to resume
notifications after the operation specified in the resume
token. The ``resume_after`` modifier takes a value that must
resolve to a resume token, e.g. ``resume_token`` in the
example below.
.. literalinclude:: /driver-examples/change_stream_examples_spec.rb
:language: ruby
:dedent: 6
:start-after: Start Changestream Example 3
:end-before: End Changestream Example 3
.. tab::
:tabid: go
You can use `ChangeStreamOptions.SetResumeAfter