@@ -643,6 +643,11 @@ Change streams are resumable by specifying a resume token to either
643
643
``resumeAfter`` for Change Streams
644
644
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
645
645
646
+ You can resume a change stream after a specific event by passing a resume token
647
+ to ``resumeAfter`` when opening the cursor.
648
+
649
+ See :ref:`change-stream-resume-token` for more information on the resume token.
650
+
646
651
.. container::
647
652
648
653
.. tabs-pillstrip:: languages
@@ -822,9 +827,9 @@ You can start a new change stream after a specific event by passing a resume
822
827
token to ``startAfter`` when opening the cursor. Unlike
823
828
:ref:`resumeAfter <change-stream-resume-after>`, ``startAfter`` can
824
829
resume notifications after an :ref:`invalidate event <change-event-invalidate>`
825
- by creating a new change stream. For the resume token, use the ``_id`` value of
826
- the :ref:`change stream event document <change-stream-output>`. See
827
- :ref:`change-stream-resume-token` for more information on the resume token.
830
+ by creating a new change stream.
831
+
832
+ See :ref:`change-stream-resume-token` for more information on the resume token.
828
833
829
834
.. important::
830
835
@@ -837,22 +842,133 @@ the :ref:`change stream event document <change-stream-output>`. See
837
842
Resume Tokens
838
843
~~~~~~~~~~~~~
839
844
840
- The ``_id`` value of a :ref:`change stream event document
841
- <change-stream-output>` acts as the resume token:
845
+ The resume token is available from multiple sources:
846
+
847
+ .. list-table::
848
+ :header-rows: 1
849
+ :stub-columns: 1
850
+ :widths: 20 80
851
+
852
+ * - Source
853
+ - Description
854
+
855
+ * - :ref:`Change Events <change-event-resume-token>`
856
+ - Each change event notification includes a resume token
857
+ on the ``_id`` field.
858
+
859
+ * - :ref:`Aggregation <aggregate-resume-token>`
860
+ - The :pipeline:`$changeStream` aggregation stage includes
861
+ a resume token on the ``cursor.postBatchResumeToken`` field.
862
+
863
+ This field only appears when using the :dbcommand:`aggregate`
864
+ command.
865
+
866
+ * - :ref:`Get More <getMore-resume-token>`
867
+ - The :dbcommand:`getMore` command includes a resume token on the
868
+ ``cursor.postBatchResumeToken`` field.
869
+
870
+ .. versionchanged:: 4.2
871
+
872
+ .. include:: /includes/extracts/4.2-changes-change-stream-modification-error.rst
873
+
874
+ .. _change-event-resume-token:
875
+
876
+ Resume Tokens from Change Events
877
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
878
+
879
+ Change event notifications include a resume token on the ``_id`` field:
880
+
881
+ .. code-block:: json
882
+ :copyable: false
883
+ :emphasize-lines: 2-4
842
884
843
- .. code-block:: none
844
885
845
886
{
846
- "_data" : <BinData|hex string>
887
+ "_id": {
888
+ "_data": "82635019A0000000012B042C0100296E5A1004AB1154ACACD849A48C61756D70D3B21F463C6F7065726174696F6E54797065003C696E736572740046646F63756D656E744B65790046645F69640064635019A078BE67426D7CF4D2000004"
889
+ },
890
+ "operationType": "insert",
891
+ "clusterTime": Timestamp({ "t": 1666193824, "i": 1 }),
892
+ "collectionUUID": new UUID("ab1154ac-acd8-49a4-8c61-756d70d3b21f"),
893
+ "fullDocument": {
894
+ "_id": ObjectId("635019a078be67426d7cf4d2"'),
895
+ "name": "Giovanni Verga"
896
+ },
897
+ "ns": {
898
+ "db": "test",
899
+ "coll": "names"
900
+ },
901
+ "documentKey": {
902
+ "_id": ObjectId("635019a078be67426d7cf4d2")
903
+ }
847
904
}
848
905
849
- .. include:: /includes/extracts/changestream-resume-token-versions-4.2-greater.rst
850
906
851
- .. include:: /includes/extracts/changestream -resume-token-hex-change.rst
907
+ .. _aggregate -resume-token:
852
908
853
- .. tip::
909
+ Resume Tokens from ``aggregate``
910
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
911
+
912
+ When using the :dbcommand:`aggregate` command, the :pipeline:`$changeStream`
913
+ aggregation stage includes a resume token on the
914
+ ``cursor.postBatchResumeToken`` field:
915
+
916
+ .. code-block:: json
917
+ :copyable: false
918
+ :emphasize-lines: 4-6
919
+
920
+ {
921
+ "cursor": {
922
+ "firstBatch": [],
923
+ "postBatchResumeToken": {
924
+ "_data": "8263515EAC000000022B0429296E1404"
925
+ },
926
+ "id": Long("4309380460777152828"),
927
+ "ns": "test.names"
928
+ },
929
+ "ok": 1,
930
+ "$clusterTime": {
931
+ "clusterTime": Timestamp({ "t": 1666277036, "i": 1 }),
932
+ "signature": {
933
+ "hash": Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
934
+ "keyId": Long("0")
935
+ }
936
+ },
937
+ "operationTime": Timestamp({ "t": 1666277036, "i": 1 })
938
+ }
939
+
940
+ .. _getMore-resume-token:
941
+
942
+ Resume Tokens from ``getMore``
943
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
944
+
945
+ The :dbcommand:`getMore` command also includes a resume token on the
946
+ ``cursor.postBatchResumeToken`` field:
947
+
948
+ .. code-block:: json
949
+ :copyable: false
950
+ :emphasize-lines: 4-6
951
+
952
+ {
953
+ "cursor": {
954
+ "nextBatch": [],
955
+ "postBatchResumeToken": {
956
+ "_data": "8263515979000000022B0429296E1404"
957
+ },
958
+ "id": Long("7049907285270685005"),
959
+ "ns": "test.names"
960
+ },
961
+ "ok": 1,
962
+ "$clusterTime": {
963
+ "clusterTime": Timestamp( { "t": 1666275705, "i": 1 } ),
964
+ "signature": {
965
+ "hash": Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
966
+ "keyId": Long("0")
967
+ }
968
+ },
969
+ "operationTime": Timestamp({ "t": 1666275705, "i": 1 })
970
+ }
854
971
855
- .. include:: /includes/extracts/4.2-changes-change-stream-modification-error.rst
856
972
857
973
Use Cases
858
974
---------
0 commit comments