|
| 1 | +.. _javars-changestream: |
| 2 | + |
| 3 | +============== |
| 4 | +Change Streams |
| 5 | +============== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +MongoDB 3.6 introduces the ``$changeStream`` aggregation pipeline |
| 14 | +operator. |
| 15 | + |
| 16 | +Change streams provide a way to watch changes to documents in a |
| 17 | +collection. To improve the usability of this new stage, the |
| 18 | +``MongoCollection`` type includes a new ``watch()`` method. The |
| 19 | +``ChangeStreamPublisher`` instance sets up the change stream and automatically |
| 20 | +attempts to resume if it encounters a potentially recoverable error. |
| 21 | + |
| 22 | +Prerequisites |
| 23 | +------------- |
| 24 | + |
| 25 | +You must set up the following components to run the code examples in |
| 26 | +this guide: |
| 27 | + |
| 28 | +- A ``test.restaurants`` collection populated with documents from the |
| 29 | + ``restaurants.json`` file in the `documentation assets GitHub |
| 30 | + <https://raw.githubusercontent.com/mongodb/docs-assets/drivers/restaurants.json>`__. |
| 31 | + |
| 32 | +- The following import statements: |
| 33 | + |
| 34 | +.. code-block:: java |
| 35 | + |
| 36 | + import com.mongodb.reactivestreams.client.MongoClients; |
| 37 | + import com.mongodb.reactivestreams.client.MongoClient; |
| 38 | + import com.mongodb.reactivestreams.client.MongoCollection; |
| 39 | + import com.mongodb.reactivestreams.client.MongoDatabase; |
| 40 | + |
| 41 | + import com.mongodb.client.model.Aggregates; |
| 42 | + import com.mongodb.client.model.Filters; |
| 43 | + import com.mongodb.client.model.changestream.FullDocument; |
| 44 | + import com.mongodb.client.model.changestream.ChangeStreamDocument; |
| 45 | + |
| 46 | + import org.bson.Document; |
| 47 | + |
| 48 | +.. important:: |
| 49 | + |
| 50 | + This guide uses the ``Subscriber`` implementations, which are |
| 51 | + described in the :ref:`Quick Start Primer <javars-primer>`. |
| 52 | + |
| 53 | +Connect to a MongoDB Deployment |
| 54 | +------------------------------- |
| 55 | + |
| 56 | +First, connect to a MongoDB deployment and declare and define |
| 57 | +``MongoDatabase`` and ``MongoCollection`` instances. |
| 58 | + |
| 59 | +The following code connects to a standalone |
| 60 | +MongoDB deployment running on ``localhost`` on port ``27017``. Then, it |
| 61 | +defines the ``database`` variable to refer to the ``test`` database and |
| 62 | +the ``collection`` variable to refer to the ``restaurants`` collection: |
| 63 | + |
| 64 | +.. code-block:: java |
| 65 | + |
| 66 | + MongoClient mongoClient = MongoClients.create(); |
| 67 | + MongoDatabase database = mongoClient.getDatabase("test"); |
| 68 | + MongoCollection<Document> collection = database.getCollection("restaurants"); |
| 69 | + |
| 70 | +To learn more about connecting to MongoDB deployments, |
| 71 | +see the :ref:`javars-connect` tutorial. |
| 72 | + |
| 73 | +Watch for Changes on a Collection |
| 74 | +--------------------------------- |
| 75 | + |
| 76 | +To create a change stream use one of the ``MongoCollection.watch()`` |
| 77 | +methods. |
| 78 | + |
| 79 | +In the following example, the change stream prints out all changes it |
| 80 | +observes: |
| 81 | + |
| 82 | +.. code-block:: java |
| 83 | + |
| 84 | + collection.watch().subscribe(new PrintDocumentSubscriber()); |
| 85 | + |
| 86 | +Watch for Changes on a Database |
| 87 | +------------------------------- |
| 88 | + |
| 89 | +Applications can open a single change stream to watch all non-system |
| 90 | +collections of a database. To create such a change stream, use one of the |
| 91 | +``MongoDatabase.watch()`` methods. |
| 92 | + |
| 93 | +In the following example, the change stream prints out all the changes |
| 94 | +it observes on the given database: |
| 95 | + |
| 96 | +.. code-block:: java |
| 97 | + |
| 98 | + database.watch().subscribe(new PrintDocumentSubscriber()); |
| 99 | + |
| 100 | +Watch for Changes on All Databases |
| 101 | +---------------------------------- |
| 102 | + |
| 103 | +Applications can open a single change stream to watch all non-system |
| 104 | +collections of all databases in a MongoDB deployment. To create such a |
| 105 | +change stream, use one of the ``MongoClient.watch()`` methods. |
| 106 | + |
| 107 | +In the following example, the change stream prints out all the changes |
| 108 | +it observes on the deployment to which the ``MongoClient`` is connected: |
| 109 | + |
| 110 | +.. code-block:: java |
| 111 | + |
| 112 | + client.watch().subscribe(new PrintDocumentSubscriber()); |
| 113 | + |
| 114 | +Filtering Content |
| 115 | +----------------- |
| 116 | + |
| 117 | +You can pass a list of aggregation stages to the ``watch()`` method to |
| 118 | +modify the data returned by the ``$changeStream`` operator. |
| 119 | + |
| 120 | +.. note:: |
| 121 | + |
| 122 | + Not all aggregation operators are supported. See |
| 123 | + :manual:`Change Streams </changeStreams/>` in the Server manual to learn more. |
| 124 | + |
| 125 | +In the following example, the change stream prints out all changes it |
| 126 | +observes corresponding to ``insert``, ``update``, ``replace`` and |
| 127 | +``delete`` operations. |
| 128 | + |
| 129 | +First, the pipeline includes a ``$match`` stage to filter for documents |
| 130 | +where the ``operationType`` is either an ``insert``, ``update``, ``replace`` or |
| 131 | +``delete``. Then, it sets the ``fullDocument`` to |
| 132 | +``FullDocument.UPDATE_LOOKUP``, so that the document after the update is |
| 133 | +included in the results: |
| 134 | + |
| 135 | +.. code-block:: java |
| 136 | + |
| 137 | + collection.watch( |
| 138 | + asList( |
| 139 | + Aggregates.match( |
| 140 | + Filters.in("operationType", asList("insert", "update", "replace", "delete")) |
| 141 | + ) |
| 142 | + ) |
| 143 | + ).fullDocument(FullDocument.UPDATE_LOOKUP).subscribe(new PrintDocumentSubscriber()); |
0 commit comments