55Monitor Data Changes
66====================
77
8+ .. facet::
9+ :name: genre
10+ :values: reference
11+
812.. meta::
9- :description: Learn about opening change streams and monitoring data changes in MongoDB by using the {+driver-long+}.
13+ :keywords: code example, delta
1014
1115.. contents:: On this page
1216 :local:
@@ -17,19 +21,27 @@ Monitor Data Changes
1721Overview
1822--------
1923
20- In this guide, you can learn how to monitor document changes with a change stream.
24+ In this guide, you can learn how to monitor document changes by using a ** change stream** .
2125
2226A change stream outputs new change events, providing access to real-time data changes.
2327You can open a change stream on a collection, database, or client object.
2428
2529Sample Data
2630~~~~~~~~~~~
2731
32+ The examples in this guide use the following ``Course`` struct as a model for documents
33+ in the ``courses`` collection:
34+
35+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
36+ :start-after: begin struct
37+ :end-before: end struct
38+ :language: go
39+ :dedent:
40+
2841To run the examples in this guide, load these documents into the
29- ``db.courses`` collection with the following
30- snippet:
42+ ``courses`` collection in the ``db`` database with the following snippet:
3143
32- .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort .go
44+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream .go
3345 :language: go
3446 :dedent:
3547 :start-after: begin insertDocs
@@ -56,29 +68,19 @@ empty ``Pipeline`` object.
5668Example
5769~~~~~~~
5870
59- The following example opens a change stream on the ``db. courses`` collection and
71+ The following example opens a change stream on the ``courses`` collection and
6072outputs all changes:
6173
62- .. code-block:: go
63-
64- coll := client.Database("db").Collection("courses")
65-
66- // open a change stream with an empty pipeline parameter
67- changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{})
68- if err != nil {
69- panic(err)
70- }
71- defer changeStream.Close(context.TODO())
72-
73- // iterate over the cursor to print the change-stream events
74- for changeStream.Next(context.TODO()) {
75- fmt.Println(changeStream.Current)
76- }
74+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
75+ :language: go
76+ :dedent:
77+ :start-after: begin open stream
78+ :end-before: end open stream
7779
78- If you modify the ``db. courses`` collection in a separate program or shell, this code will print
80+ If you modify the ``courses`` collection in a separate program or shell, this code will print
7981your changes as they occur. Inserting a document with a ``title`` value
80- of "Advanced Screenwriting" and an ``enrollment`` value of ``20``
81- results in the following change-stream event:
82+ of `` "Advanced Screenwriting"`` and an ``enrollment`` value of ``20``
83+ results in the following change event:
8284
8385.. code-block:: none
8486 :copyable: false
@@ -108,20 +110,19 @@ You can use the following pipeline stages in this parameter:
108110Example
109111~~~~~~~
110112
111- The following example opens a change stream on the ``db`` database, but only watches for
113+ The following example opens a change stream on the ``db`` database but only watches for
112114new delete operations:
113115
114- .. code-block:: go
115-
116- db := client.Database("db")
117-
118- pipeline := bson.D{{"$match", bson.D{{"operationType", "delete"}}}}
119- changeStream, err := db.Watch(context.TODO(), mongo.Pipeline{pipeline})
116+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
117+ :language: go
118+ :dedent:
119+ :start-after: begin delete events
120+ :end-before: end delete events
120121
121122.. note::
122123
123124 The ``Watch()`` method was called on the ``db`` database, so the code outputs
124- new delete operations in any collection within this database.
125+ new delete operations on any collection within this database.
125126
126127Modify the Behavior of ``Watch()``
127128----------------------------------
@@ -194,28 +195,19 @@ document before a change, set the ``FullDocumentBeforeChange`` field of the
194195Example
195196~~~~~~~
196197
197- The following example calls the ``Watch()`` method on the ``db. courses`` collection. It
198+ The following example calls the ``Watch()`` method on the ``courses`` collection. It
198199specifies a value for the ``FullDocument`` field of the ``options`` parameter to
199200output a copy of the entire modified document, instead of only the changed fields:
200201
201- .. code-block:: go
202-
203- coll := client.Database("db").Collection("courses")
204- opts := options.ChangeStream().SetFullDocument(options.UpdateLookup)
205-
206- changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{}, opts)
207- if err != nil {
208- panic(err)
209- }
210- defer changeStream.Close(context.TODO())
211-
212- for changeStream.Next(context.TODO()) {
213- fmt.Println(changeStream.Current)
214- }
202+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
203+ :language: go
204+ :dedent:
205+ :start-after: begin full document
206+ :end-before: end full document
215207
216208Updating the ``enrollment`` value of the document with the
217- ``title`` of "World Fiction" from ``35`` to ``30`` results in the
218- following change-stream event:
209+ ``title`` of `` "World Fiction"`` from ``35`` to ``30`` results in the
210+ following change event:
219211
220212.. code-block:: none
221213 :copyable: false
0 commit comments