5
5
Monitor Data Changes
6
6
====================
7
7
8
+ .. facet::
9
+ :name: genre
10
+ :values: reference
11
+
8
12
.. meta::
9
- :description: Learn about opening change streams and monitoring data changes in MongoDB by using the {+driver-long+}.
13
+ :keywords: code example, delta
10
14
11
15
.. contents:: On this page
12
16
:local:
@@ -17,19 +21,27 @@ Monitor Data Changes
17
21
Overview
18
22
--------
19
23
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** .
21
25
22
26
A change stream outputs new change events, providing access to real-time data changes.
23
27
You can open a change stream on a collection, database, or client object.
24
28
25
29
Sample Data
26
30
~~~~~~~~~~~
27
31
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
+
28
41
To 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:
31
43
32
- .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort .go
44
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream .go
33
45
:language: go
34
46
:dedent:
35
47
:start-after: begin insertDocs
@@ -56,29 +68,19 @@ empty ``Pipeline`` object.
56
68
Example
57
69
~~~~~~~
58
70
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
60
72
outputs all changes:
61
73
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
77
79
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
79
81
your 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:
82
84
83
85
.. code-block:: none
84
86
:copyable: false
@@ -108,20 +110,19 @@ You can use the following pipeline stages in this parameter:
108
110
Example
109
111
~~~~~~~
110
112
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
112
114
new delete operations:
113
115
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
120
121
121
122
.. note::
122
123
123
124
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.
125
126
126
127
Modify the Behavior of ``Watch()``
127
128
----------------------------------
@@ -194,28 +195,19 @@ document before a change, set the ``FullDocumentBeforeChange`` field of the
194
195
Example
195
196
~~~~~~~
196
197
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
198
199
specifies a value for the ``FullDocument`` field of the ``options`` parameter to
199
200
output a copy of the entire modified document, instead of only the changed fields:
200
201
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
215
207
216
208
Updating 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:
219
211
220
212
.. code-block:: none
221
213
:copyable: false
0 commit comments