Skip to content

Commit 13a2f7f

Browse files
committed
PHPLIB-435: Document ChangeStream::getResumeToken()
1 parent e651f9a commit 13a2f7f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
=======================================
2+
MongoDB\\ChangeStream::getResumeToken()
3+
=======================================
4+
5+
.. versionadded:: 1.5
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\ChangeStream::getResumeToken()
19+
20+
Returns the cached resume token that will be used to resume the change
21+
stream.
22+
23+
.. code-block:: php
24+
25+
function getResumeToken(): array|object|null
26+
27+
Return Values
28+
-------------
29+
30+
An array or object, or ``null`` if there is no cached resume token. The return
31+
type will depend on the ``typeMap`` option for the ``watch()`` method used to
32+
create the change stream.
33+
34+
Examples
35+
--------
36+
37+
This example captures the resume token for a change stream after encountering
38+
an ``invalidate`` event and uses it to construct a second change stream using
39+
the ``startAfter`` option.
40+
41+
.. code-block:: php
42+
43+
<?php
44+
45+
$uri = 'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet';
46+
47+
$collection = (new MongoDB\Client($uri))->test->inventory;
48+
49+
$changeStream = $collection->watch();
50+
51+
for ($changeStream->rewind(); true; $changeStream->next()) {
52+
if ( ! $changeStream->valid()) {
53+
continue;
54+
}
55+
56+
$event = $changeStream->current();
57+
58+
if ($event['operationType'] === 'invalidate') {
59+
$startAfter = $changeStream->getResumeToken();
60+
break;
61+
}
62+
63+
printf("%d: %s\n", $changeStream->key(), $event['operationType']);
64+
}
65+
66+
$changeStream = $collection->watch([], ['startAfter' => $startAfter]);
67+
68+
See Also
69+
--------
70+
71+
- :phpmethod:`MongoDB\\Client::watch()`
72+
- :phpmethod:`MongoDB\\Collection::watch()`
73+
- :phpmethod:`MongoDB\\Database::watch()`
74+
- :manual:`Resume a Change Stream </changeStreams#resume-a-change-stream>`
75+
documentation in the MongoDB manual

docs/reference/result-classes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Methods
3636

3737
/reference/method/MongoDBChangeStream-current
3838
/reference/method/MongoDBChangeStream-getCursorId
39+
/reference/method/MongoDBChangeStream-getResumeToken
3940
/reference/method/MongoDBChangeStream-key
4041
/reference/method/MongoDBChangeStream-next
4142
/reference/method/MongoDBChangeStream-rewind

0 commit comments

Comments
 (0)