MongoDB\ChangeStream::current()

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

MongoDB\ChangeStream::current()

Definition

Returns the current event in the change stream.

function current(): array|object|null

The structure of each event document will vary based on the operation type. See Change Events in the MongoDB manual for more information.

Return Values

An array or object for the current event in the change stream, or null if there is no current event (i.e. MongoDB\ChangeStream::valid(). The return type will depend on the typeMap option for [MongoDB\Collection::watch().](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-watch.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--watch--

Examples

This example reports events while iterating a change stream.

<?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();

    $ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']);
    $id = json_encode($event['documentKey']['_id']);

    switch ($event['operationType']) {
        case 'delete':
            printf("Deleted document in %s with _id: %s\n\n", $ns, $id);
            break;

        case 'insert':
            printf("Inserted new document in %s\n", $ns);
            echo json_encode($event['fullDocument']), "\n\n";
            break;

        case 'replace':
            printf("Replaced new document in %s with _id: %s\n", $ns, $id);
            echo json_encode($event['fullDocument']), "\n\n";
            break;

        case 'update':
            printf("Updated document in %s with _id: %s\n", $ns, $id);
            echo json_encode($event['updateDescription']), "\n\n";
            break;
    }
}

Assuming that a document was inserted, updated, and deleted while the above script was iterating the change stream, the output would then resemble:

Inserted new document in test.inventory
{"_id":{"$oid":"5a81fc0d6118fd1af1790d32"},"name":"Widget","quantity":5}

Updated document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}
{"updatedFields":{"quantity":4},"removedFields":[]}

Deleted document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}

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--

  • Iterator::current()

  • [Tailable Cursor Iteration](https://www.mongodb.com/docs/php-library/current/crud/query/cursor.md#std-label-php-tailable-cursor

  • Change Streams documentation in the MongoDB manual

  • Change Events documentation in the MongoDB manual