MongoDBClient-watch.txt 3.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
========================
MongoDB\\Client::watch()
========================

.. versionadded:: 1.4

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\Client::watch()

   Executes a :manual:`change stream </changeStreams>` operation on the client.
   The change stream can be watched for cluster-level changes.

   .. code-block:: php

      function watch(array $pipeline = [], array $options = []): MongoDB\ChangeStream

   This method has the following parameters:

   .. include:: /includes/apiargs/MongoDBClient-method-watch-param.rst

   The ``$options`` parameter supports the following options:

   .. include:: /includes/apiargs/MongoDBClient-method-watch-option.rst

Return Values
-------------

A :phpclass:`MongoDB\\ChangeStream` object, which allows for iteration of
events in the change stream via the :php:`Iterator <class.iterator>` interface.

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Examples
--------

This example reports events while iterating a change stream.

.. code-block:: php

   <?php

   $uri = 'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet';

   $client = new MongoDB\Client($uri);

   $changeStream = $client->watch();

   for ($changeStream->rewind(); true; $changeStream->next()) {
       if ( ! $changeStream->valid()) {
           continue;
       }

       $event = $changeStream->current();

       if ($event['operationType'] === 'invalidate') {
           break;
       }

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

.. code-block:: none

   Inserted new document in app.user
   {"_id":{"$oid":"5b329b6674083047cc05e607"},"username":"bob"}

   Inserted new document in app.products
   {"_id":{"$oid":"5b329b6a74083047cc05e608"},"name":"Widget","quantity":5}

   Inserted new document in logs.messages
   {"_id":{"$oid":"5b329b7374083047cc05e609"},"msg":"bob purchased a widget"}

See Also
--------

117 118
- :phpmethod:`MongoDB\\Collection::watch()`
- :phpmethod:`MongoDB\\Database::watch()`
119 120 121 122 123
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
  the MongoDB Manual
- :manual:`Change Streams </changeStreams>` documentation in the MongoDB manual
- :manual:`Change Events </reference/change-events/>` documentation in the
  MongoDB manual