MongoDB\ChangeStream::getResumeToken()
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
MongoDB\ChangeStream::getResumeToken()
New in version 1.5
Definition
Returns the cached resume token that will be used to resume the change stream.
function getResumeToken(): array|object|null
Return Values
An array or object, or null if there is no cached resume token. The return type will depend on the typeMap option for the watch() method used to create the change stream.
Examples
This example captures the resume token for a change stream after encountering an invalidate event and uses it to construct a second change stream using the startAfter option.
<?php
$uri = 'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet';
$collection = (new MongoDB\Client($uri))->test->inventory;
$changeStream = $collection->watch();
for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
if ($event['operationType'] === 'invalidate') {
$startAfter = $changeStream->getResumeToken();
break;
}
printf("%d: %s\n", $changeStream->key(), $event['operationType']);
}
$changeStream = $collection->watch([], ['startAfter' => $startAfter]);
See Also
-
[
MongoDB\Client::watch()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbclient-watch.md#mongodb-phpmethod-phpmethod.MongoDB-Client--watch-- -
[
MongoDB\Collection::watch()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-watch.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--watch-- -
[
MongoDB\Database::watch()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbdatabase-watch.md#mongodb-phpmethod-phpmethod.MongoDB-Database--watch-- -
[Resume a Change Stream](https://www.mongodb.com/docs/manual/changeStreams/#resume-a-change-stream documentation in the MongoDB manual
