|
| 1 | +================================ |
| 2 | +MongoDB\\ChangeStream::current() |
| 3 | +================================ |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. phpmethod:: MongoDB\\ChangeStream::current() |
| 17 | + |
| 18 | + Returns the current event in the change stream. |
| 19 | + |
| 20 | + .. code-block:: php |
| 21 | + |
| 22 | + function current(): array|object|null |
| 23 | + |
| 24 | + The structure of each event document will vary based on the operation type. |
| 25 | + See :manual:`Change Events </reference/change-events/>` in the MongoDB manual |
| 26 | + for more information. |
| 27 | + |
| 28 | +Return Values |
| 29 | +------------- |
| 30 | + |
| 31 | +An array or object for the current event in the change stream, or ``null`` if |
| 32 | +there is no current event (i.e. :phpmethod:`MongoDB\\ChangeStream::valid()` |
| 33 | +returns ``false``). The return type will depend on the ``typeMap`` option for |
| 34 | +:phpmethod:`MongoDB\\Collection::watch()`. |
| 35 | + |
| 36 | +Examples |
| 37 | +-------- |
| 38 | + |
| 39 | +This example reports events while iterating a change stream. |
| 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 | + $ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']); |
| 59 | + $id = json_encode($event['documentKey']['_id']); |
| 60 | + |
| 61 | + switch ($event['operationType']) { |
| 62 | + case 'delete': |
| 63 | + printf("Deleted document in %s with _id: %s\n\n", $ns, $id); |
| 64 | + break; |
| 65 | + |
| 66 | + case 'insert': |
| 67 | + printf("Inserted new document in %s\n", $ns); |
| 68 | + echo json_encode($event['fullDocument']), "\n\n"; |
| 69 | + break; |
| 70 | + |
| 71 | + case 'replace': |
| 72 | + printf("Replaced new document in %s with _id: %s\n", $ns, $id); |
| 73 | + echo json_encode($event['fullDocument']), "\n\n"; |
| 74 | + break; |
| 75 | + |
| 76 | + case 'update': |
| 77 | + printf("Updated document in %s with _id: %s\n", $ns, $id); |
| 78 | + echo json_encode($event['updateDescription']), "\n\n"; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +Assuming that a document was inserted, updated, and deleted while the above |
| 84 | +script was iterating the change stream, the output would then resemble: |
| 85 | + |
| 86 | +.. code-block:: none |
| 87 | + |
| 88 | + Inserted new document in test.inventory |
| 89 | + {"_id":{"$oid":"5a81fc0d6118fd1af1790d32"},"name":"Widget","quantity":5} |
| 90 | + |
| 91 | + Updated document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"} |
| 92 | + {"updatedFields":{"quantity":4},"removedFields":[]} |
| 93 | + |
| 94 | + Deleted document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"} |
| 95 | + |
| 96 | +See Also |
| 97 | +-------- |
| 98 | + |
| 99 | +- :phpmethod:`MongoDB\\Collection::watch()` |
| 100 | +- :php:`Iterator::current() <iterator.current>` |
| 101 | +- :ref:`Tailable Cursor Iteration <php-tailable-cursor>` |
| 102 | +- :manual:`Change Streams </changeStreams>` documentation in the MongoDB manual |
| 103 | +- :manual:`Change Events </reference/change-events/>` documentation in the |
| 104 | + MongoDB manual |
0 commit comments