|
| 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 |
0 commit comments