@@ -24,7 +24,7 @@ Sample Data
24
24
~~~~~~~~~~~
25
25
26
26
To run the examples in this guide, load these documents into the
27
- ``tea.ratings `` collection with the following
27
+ ``db.courses `` collection with the following
28
28
snippet:
29
29
30
30
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort.go
@@ -39,13 +39,14 @@ snippet:
39
39
you perform a write operation, the server implicitly creates
40
40
them.
41
41
42
- Each document contains a rating for a type of tea that corresponds to the ``type``
43
- and ``rating`` fields.
42
+ Each document contains a description of a university course that
43
+ includes the course title and maximum enrollment, corresponding to
44
+ the ``title`` and ``enrollment`` fields in each document.
44
45
45
46
.. note::
46
47
47
- Each example truncates the ``_data``, ``clusterTime``, and ``ObjectID`` values
48
- because the driver generates them uniquely.
48
+ Each example output shows truncated ``_data``, ``clusterTime``, and
49
+ ``ObjectID`` values because the driver generates them uniquely.
49
50
50
51
Open a Change Stream
51
52
--------------------
@@ -56,12 +57,12 @@ parameter and a pipeline parameter. To return all changes, pass in an empty Pipe
56
57
Example
57
58
~~~~~~~
58
59
59
- The following example opens a change stream on the ``tea.ratings `` collection and
60
+ The following example opens a change stream on the ``db.courses `` collection and
60
61
outputs all changes:
61
62
62
63
.. code-block:: go
63
64
64
- coll := client.Database("tea ").Collection("ratings ")
65
+ coll := client.Database("db ").Collection("courses ")
65
66
66
67
// open a change stream with an empty pipeline parameter
67
68
changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{})
@@ -70,21 +71,22 @@ outputs all changes:
70
71
}
71
72
defer changeStream.Close(context.TODO())
72
73
73
- // iterate over the cursor to print the change stream events
74
+ // iterate over the cursor to print the change- stream events
74
75
for changeStream.Next(context.TODO()) {
75
76
fmt.Println(changeStream.Current)
76
77
}
77
78
78
- If you modify the ``tea.ratings`` collection in a separate shell, this code will print
79
- your changes. Inserting a document with a ``type`` value of ``"White Peony"`` and a
80
- ``rating`` value of ``4`` will output the following change event:
79
+ If you modify the ``db.courses`` collection in a separate program or shell, this code will print
80
+ your changes as they occur. Inserting a document with a ``title`` value
81
+ of "Advanced Screenwriting" and an ``enrollment`` value of ``20``
82
+ results in the following change-stream event:
81
83
82
84
.. code-block:: go
83
85
:copyable: false
84
86
85
87
map[_id:map[_data:...] clusterTime: {...} documentKey:map[_id:ObjectID("...")]
86
- fullDocument:map[_id:ObjectID("...") rating:4 type:White Peony ] ns:
87
- map[coll:ratings db:tea ] operationType:insert]
88
+ fullDocument:map[_id:ObjectID("...") enrollment:20 title:Advanced Screenwriting ] ns:
89
+ map[coll:courses db:db ] operationType:insert]
88
90
89
91
Modify the Change Stream Output
90
92
-------------------------------
@@ -107,12 +109,12 @@ You can use the following pipeline stages in this parameter:
107
109
Example
108
110
~~~~~~~
109
111
110
- The following example opens a change stream on the ``tea `` database, but only watches for
112
+ The following example opens a change stream on the ``db `` database, but only watches for
111
113
new delete operations:
112
114
113
115
.. code-block:: go
114
116
115
- db := client.Database("tea ")
117
+ db := client.Database("db ")
116
118
117
119
pipeline := bson.D{{"$match", bson.D{{"operationType", "delete"}}}}
118
120
changeStream, err := db.Watch(context.TODO(), mongo.Pipeline{pipeline})
@@ -125,21 +127,20 @@ new delete operations:
125
127
fmt.Println(changeStream.Current)
126
128
}
127
129
128
- If you delete the ``tea.ratings`` document with a ``type `` value of
129
- ``"White Peony"`` in a separate shell, this code will output the following:
130
+ Deleting the document with the ``title `` value of "Advanced Screenwriting"
131
+ in a separate program or shell results in the following change-stream event :
130
132
131
133
.. code-block:: go
132
134
:copyable: false
133
135
134
136
{"_id": {"_data": "..."},"operationType": "delete","clusterTime":
135
- {"$timestamp":{"t":"...","i":"..."}},"ns": {"db": "tea ","coll": "ratings "},
137
+ {"$timestamp":{"t":"...","i":"..."}},"ns": {"db": "db ","coll": "courses "},
136
138
"documentKey": {"_id": {"$oid":"..."}}}
137
139
138
140
.. note::
139
141
140
- The ``Watch()`` method was called on the ``tea`` database, so the code outputs
141
- new delete operations in any ``tea`` collection.
142
-
142
+ The ``Watch()`` method was called on the ``db`` database, so the code outputs
143
+ new delete operations in any collection within this database.
143
144
144
145
Modify the Behavior of Watch()
145
146
------------------------------
@@ -162,12 +163,12 @@ For more information on these fields, visit the
162
163
Example
163
164
~~~~~~~
164
165
165
- The following example calls the ``Watch()`` method on the ``tea.ratings `` collection. It
166
- specifies the ``FullDocument`` opts parameter to output a copy of the entire modified document:
166
+ The following example calls the ``Watch()`` method on the ``db.courses `` collection. It
167
+ specifies the ``FullDocument`` options parameter to output a copy of the entire modified document:
167
168
168
169
.. code-block:: go
169
170
170
- coll := client.Database("tea ").Collection("ratings ")
171
+ coll := client.Database("db ").Collection("courses ")
171
172
opts := options.ChangeStream().SetFullDocument(options.UpdateLookup)
172
173
173
174
changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{}, opts)
@@ -180,16 +181,18 @@ specifies the ``FullDocument`` opts parameter to output a copy of the entire mod
180
181
fmt.Println(changeStream.Current)
181
182
}
182
183
183
- If you update the ``rating`` value of the ``"Masala"`` tea from ``10`` to ``9``,
184
- this code outputs the following:
184
+ Updating the ``enrollment`` value of the document with the
185
+ ``title`` of "World Fiction" from ``35`` to ``30`` results in the
186
+ following change-stream event:
185
187
186
188
.. code-block:: go
187
189
:copyable: false
188
190
189
191
{"_id": {"_data": "..."},"operationType": "update","clusterTime": {"$timestamp":
190
- {"t":"...","i":"..."}},"fullDocument": {"_id": {"$oid":"..."},"type": "Masala","rating":
191
- {"$numberInt":"9"}}, "ns": {"db": "tea","coll": "ratings"},"documentKey": {"_id":
192
- {"$oid":"..."}}, "updateDescription": {"updatedFields": {"rating": {"$numberInt":"9"}},
192
+ {"t":"...","i":"..."}},"fullDocument": {"_id":
193
+ {"$oid":"..."},"title": "World Fiction","enrollment":
194
+ {"$numberInt":"30"}}, "ns": {"db": "db","coll": "courses"},"documentKey": {"_id":
195
+ {"$oid":"..."}}, "updateDescription": {"updatedFields": {"enrollment": {"$numberInt":"30"}},
193
196
"removedFields": [],"truncatedArrays": []}}
194
197
195
198
Without specifying the ``FullDocument`` option, the same update operation no longer
0 commit comments