Skip to content

Commit 791bca6

Browse files
Isabella Siukay-kim
authored andcommitted
DOCS-11828 document 'startAfter' option for $changeStream
1 parent fd6677b commit 791bca6

File tree

7 files changed

+141
-25
lines changed

7 files changed

+141
-25
lines changed

source/changeStreams.txt

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,19 @@ document.
491491
Resume a Change Stream
492492
----------------------
493493

494-
Change streams are resumable by specifying a ``resumeAfter`` token when
495-
opening the cursor. For the ``resumeAfter`` token, use the ``_id``
496-
value of the :ref:`change stream event document
497-
<change-stream-output>`. Passing the ``_id`` value to the change stream
498-
attempts to resume notifications starting after the specified operation.
494+
Change streams are resumable by specifying a resume token to either
495+
:ref:`resumeAfter <change-stream-resume-after>` or
496+
:ref:`startAfter <change-stream-start-after>` when opening the cursor.
497+
498+
.. _change-stream-resume-after:
499+
500+
``resumeAfter`` for Change Streams
501+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
502+
503+
You can resume a change stream after a specific event by passing a resume token
504+
to ``resumeAfter`` when opening the cursor. For the resume token, use the
505+
``_id`` value of the :ref:`change stream event document <change-stream-output>`.
506+
See :ref:`change-stream-resume-token` for more information on the resume token.
499507

500508
.. important::
501509

@@ -647,6 +655,29 @@ attempts to resume notifications starting after the specified operation.
647655
:start-after: Start Changestream Example 3
648656
:end-before: End Changestream Example 3
649657

658+
.. _change-stream-start-after:
659+
660+
``startAfter`` for Change Streams
661+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
662+
663+
.. versionadded:: 4.2
664+
665+
You can start a new change stream after a specific event by passing a resume
666+
token to ``startAfter`` when opening the cursor. Unlike
667+
:ref:`resumeAfter <change-stream-resume-after>`, ``startAfter`` can
668+
resume notifications after an :ref:`invalidate event <change-event-invalidate>`
669+
by creating a new change stream. For the resume token, use the ``_id`` value of
670+
the :ref:`change stream event document <change-stream-output>`. See
671+
:ref:`change-stream-resume-token` for more information on the resume token.
672+
673+
.. important::
674+
675+
- The oplog must have enough history to locate the operation
676+
associated with the token or the timestamp, if the timestamp is in
677+
the past.
678+
679+
.. _change-stream-resume-token:
680+
650681
Resume Token and ``featureCompatibilityVersion``
651682
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
652683

source/includes/apiargs-method-Mongo.watch-options.yaml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,40 @@ description: |
1010
``_id`` field. Pass the *entire* ``_id`` field of the change event
1111
document that represents the operation you want to resume after.
1212
13-
``resumeAfter`` is mutually exclusive with ``startAtOperationTime``.
13+
``resumeAfter`` is mutually exclusive with ``startAfter`` and
14+
``startAtOperationTime``.
1415
1516
optional: true
1617
type: document
1718
operation: Mongo.watch
1819
interface: method
1920
arg_name: field
2021
---
21-
name: fullDocument
22+
name: startAfter
2223
position: 2
24+
description: |
25+
26+
Directs :method:`Mongo.watch()` to attempt starting a new change
27+
stream after the operation specified in the resume token. Allows
28+
notifications to resume after an invalidate event.
29+
30+
Each change stream event document includes a resume token as the
31+
``_id`` field. Pass the *entire* ``_id`` field of the change event
32+
document that represents the operation you want to resume after.
33+
34+
``startAfter`` is mutually exclusive with ``resumeAfter`` and
35+
``startAtOperationTime``.
36+
37+
.. versionadded:: 4.2
38+
39+
optional: true
40+
type: document
41+
operation: Mongo.watch
42+
interface: method
43+
arg_name: field
44+
---
45+
name: fullDocument
46+
position: 3
2347
description: |
2448
2549
By default, :method:`Mongo.watch()` returns the delta of
@@ -39,7 +63,7 @@ interface: method
3963
arg_name: field
4064
---
4165
name: batchSize
42-
position: 3
66+
position: 4
4367
description: |
4468
Specifies the maximum number of change events to return in each
4569
batch of the response from the MongoDB cluster.
@@ -52,7 +76,7 @@ interface: method
5276
arg_name: field
5377
---
5478
name: maxAwaitTimeMS
55-
position: 4
79+
position: 5
5680
description: |
5781
5882
The maximum amount of time in milliseconds the server waits for new
@@ -68,7 +92,7 @@ interface: method
6892
arg_name: field
6993
---
7094
name: collation
71-
position: 5
95+
position: 6
7296
description: |
7397
Pass a :ref:`collation document <collation-document-fields>`
7498
to specify a :doc:`collation </reference/collation>` for the
@@ -80,7 +104,7 @@ interface: method
80104
arg_name: field
81105
---
82106
name: startAtOperationTime
83-
position: 6
107+
position: 7
84108
source:
85109
file: apiargs-method-db.watch-options.yaml
86110
ref: startAtOperationTime

source/includes/apiargs-method-db.collection.watch-options.yaml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,40 @@ description: |
1010
``_id`` field. Pass the *entire* ``_id`` field of the change event
1111
document that represents the operation you want to resume after.
1212
13-
``resumeAfter`` is mutually exclusive with ``startAtOperationTime``.
13+
``resumeAfter`` is mutually exclusive with ``startAfter`` and
14+
``startAtOperationTime``.
1415
1516
optional: true
1617
type: document
1718
operation: db.collection.watch
1819
interface: method
1920
arg_name: field
2021
---
21-
name: fullDocument
22+
name: startAfter
2223
position: 2
24+
description: |
25+
26+
Directs :method:`~db.collection.watch` to attempt starting a new
27+
change stream after the operation specified in the resume token.
28+
Allows notifications to resume after an invalidate event.
29+
30+
Each change stream event document includes a resume token as the
31+
``_id`` field. Pass the *entire* ``_id`` field of the change event
32+
document that represents the operation you want to resume after.
33+
34+
``startAfter`` is mutually exclusive with ``resumeAfter`` and
35+
``startAtOperationTime``.
36+
37+
.. versionadded:: 4.2
38+
39+
optional: true
40+
type: document
41+
operation: db.collection.watch
42+
interface: method
43+
arg_name: field
44+
---
45+
name: fullDocument
46+
position: 3
2347
description: |
2448
2549
By default, :method:`~db.collection.watch()` returns the delta of
@@ -39,7 +63,7 @@ interface: method
3963
arg_name: field
4064
---
4165
name: batchSize
42-
position: 3
66+
position: 4
4367
description: |
4468
Specifies the maximum number of change events to return in each
4569
batch of the response from the MongoDB cluster.
@@ -52,7 +76,7 @@ interface: method
5276
arg_name: field
5377
---
5478
name: maxAwaitTimeMS
55-
position: 4
79+
position: 5
5680
description: |
5781
5882
The maximum amount of time in milliseconds the server waits for new
@@ -68,7 +92,7 @@ interface: method
6892
arg_name: field
6993
---
7094
name: collation
71-
position: 5
95+
position: 6
7296
description: |
7397
Pass a :ref:`collation document <collation-document-fields>`
7498
to specify a :doc:`collation </reference/collation>` for the
@@ -80,7 +104,7 @@ interface: method
80104
arg_name: field
81105
---
82106
name: startAtOperationTime
83-
position: 6
107+
position: 7
84108
source:
85109
file: apiargs-method-db.watch-options.yaml
86110
ref: startAtOperationTime

source/includes/apiargs-method-db.watch-options.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,30 @@ description: |
99
``_id`` field. Pass the *entire* ``_id`` field of the change event
1010
document that represents the operation you want to resume after.
1111
12-
``resumeAfter`` is mutually exclusive with ``startAtOperationTime``.
12+
``resumeAfter`` is mutually exclusive with ``startAfter`` and
13+
``startAtOperationTime``.
14+
15+
optional: true
16+
type: document
17+
operation: db.watch
18+
interface: method
19+
arg_name: field
20+
---
21+
name: startAfter
22+
description: |
23+
24+
Directs :method:`db.watch()` to attempt starting a new change stream
25+
after the operation specified in the resume token. Allows
26+
notifications to resume after an invalidate event.
27+
28+
Each change stream event document includes a resume token as the
29+
``_id`` field. Pass the *entire* ``_id`` field of the change event
30+
document that represents the operation you want to resume after.
31+
32+
``startAfter`` is mutually exclusive with ``resumeAfter`` and
33+
``startAtOperationTime``.
34+
35+
.. versionadded:: 4.2
1336
1437
optional: true
1538
type: document
@@ -82,7 +105,8 @@ description: |
82105
check the time range of the oplog, see
83106
:method:`rs.printReplicationInfo()`.
84107
85-
``startAtOperationTime`` is mutually exclusive with ``resumeAfter``.
108+
``startAtOperationTime`` is mutually exclusive with ``resumeAfter``
109+
and ``startAfter``.
86110
87111
optional: true
88112
type: Timestamp

source/includes/extracts-changestream.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ content: |
1111
|watchmethod| uses information stored in the oplog to produce the
1212
change event description and generate a resume token associated to
1313
that operation. If the operation identified by the resume token
14-
passed to the ``resumeAfter`` option has already dropped off the
15-
:term:`oplog`, |watchmethod| cannot resume the change stream.
14+
passed to the ``resumeAfter`` or ``startAfter`` option has already
15+
dropped off the :term:`oplog`, |watchmethod| cannot resume the
16+
change stream.
1617
1718
See :ref:`change-stream-resume` for more information on
1819
resuming a change stream.
@@ -59,9 +60,11 @@ content: |
5960
ref: changestream-invalid-events
6061
content: |
6162
62-
You cannot resume a change stream after an :ref:`invalidate event
63-
<change-event-invalidate>` (for example, a collection drop or rename)
64-
closes the stream.
63+
You cannot use ``resumeAfter`` to resume a change stream after an
64+
:ref:`invalidate event <change-event-invalidate>` (for example, a collection
65+
drop or rename) closes the stream. Starting in MongoDB 4.2, you can use
66+
:ref:`startAfter <change-stream-start-after>` to start a new change
67+
stream after an :ref:`invalidate event <change-event-invalidate>`.
6568
6669
---
6770
ref: changestream-cursor-open

source/reference/method/db.collection.watch.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ Resuming a Change Stream
190190
Every document returned by a change stream cursor includes a resume
191191
token as the ``_id`` field. To resume a change stream, pass the entire
192192
``_id`` document of the change event you want to resume from to
193-
the ``resumeAfter`` option of :method:`~db.collection.watch()`.
193+
either the ``resumeAfter`` or ``startAfter`` option of
194+
:method:`~db.collection.watch()`.
194195

195196
The following operation resumes a change stream cursor against the
196197
``data.sensors`` collection using a resume token. This

source/release-notes/4.2.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ An example of the old syntax is:
151151

152152
{ $out: "stock" }
153153

154+
``startAfter`` Option for Change Streams
155+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156+
157+
MongoDB 4.2 adds ``startAfter`` as an option for :ref:`changeStreams`, which
158+
starts a new change stream after the event indicated by a resume token. With
159+
this option, you can start a change stream from an
160+
:ref:`invalidate event <change-event-invalidate>`, thereby guaranteeing no
161+
missed notifications after the previous stream was invalidated.
162+
154163
Changes Affecting Compatibility
155164
-------------------------------
156165

0 commit comments

Comments
 (0)