|
| 1 | +================= |
| 2 | +Watch for Changes |
| 3 | +================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Open a Change Stream |
| 8 | +-------------------- |
| 9 | + |
| 10 | +You can keep track of changes to data in MongoDB, such as changes to a |
| 11 | +collection, database, or deployment, by opening a :manual:`Change |
| 12 | +Stream</changeStreams/>`. A ``Change Stream`` allows applications to |
| 13 | +watch for changes to data and react to those changes. You can open a |
| 14 | +``Change Stream`` by calling the :node-api:`Collection.watch() |
| 15 | +</Collection.html#watch>`, :node-api:`Db.watch() </Db.html#watch>`, or |
| 16 | +:node-api:`MongoClient.watch()</MongoClient.html#watch>` method. |
| 17 | + |
| 18 | +The ``watch()`` method optionally takes a :manual:`pipeline |
| 19 | +</reference/operator/aggregation-pipeline/>`, an array of |
| 20 | +:manual:`stages </changeStreams/#modify-change-stream-output>`, as the |
| 21 | +first parameter to filter the :manual:`change events |
| 22 | +</reference/change-events/>` output. Also, ``watch()`` can take an |
| 23 | +additional options object as the second parameter. Set the |
| 24 | +``fullDocument`` field of the additional options object to the string |
| 25 | +``"updateLookup"`` to receive an event that contains the changes to the |
| 26 | +document as well as the entire altered document. Alternatively, you can |
| 27 | +set ``fullDocument`` to the string ``"default"`` to only receive the |
| 28 | +altered document, but not the changes to the document. |
| 29 | + |
| 30 | +Process the Change Stream Events |
| 31 | +-------------------------------- |
| 32 | + |
| 33 | +You can capture events from a ``Change Stream`` instance by using a listener |
| 34 | +function. Call the ``watch()`` command to get a ``Change Stream`` instance. Add |
| 35 | +your listener function by calling the `EventEmitter.on() |
| 36 | +<https://nodejs.org/api/events.html#events_emitter_on_eventname_listener>`_ |
| 37 | +method on that instance. Set the value of the first parameter to the |
| 38 | +string ``'change'`` and add your :mdn:`callback function |
| 39 | +<Glossary/Callback_function>` as the second parameter. The callback |
| 40 | +triggers when a ``change event`` is emitted, providing the next available |
| 41 | +document. You can specify logic in the callback to process the event |
| 42 | +document when it is received. |
| 43 | + |
| 44 | +Call the :node-api:`close() </ChangeStream.html#close>` method on the |
| 45 | +``Change Stream`` instance to stop processing ``change events``. This method |
| 46 | +closes the ``Change Stream`` and frees resources. |
| 47 | + |
| 48 | +Example |
| 49 | +------- |
| 50 | + |
| 51 | +The following example opens a ``Change Stream`` on the ``movies`` |
| 52 | +collection. We create a listener function to receive and print change |
| 53 | +events that occur. |
| 54 | + |
| 55 | +To emit a ``change event``, we perform a change to the collection, such as |
| 56 | +insertion with ``insertOne()``. To prevent ``insertOne()`` from |
| 57 | +executing before the listener function can register, we create a timer |
| 58 | +that uses :mdn:`setTimeout |
| 59 | +<Web/API/WindowOrWorkerGlobalScope/setTimeout>` to wait 1 second. When |
| 60 | +you insert a document, the listener receives the corresponding event. |
| 61 | + |
| 62 | +After that, we create a second timer to wait an additional second after |
| 63 | +the insertion of the document to close the ``Change Stream`` instance |
| 64 | +using ``changeStream.close()``. |
| 65 | + |
| 66 | +.. literalinclude:: /code-snippets/usage-examples/changeStream.js |
| 67 | + :language: javascript :linenos: |
0 commit comments