Skip to content

Commit e35b3fc

Browse files
authored
DOCSP-26445: use struct in watch for changes (#221)
* DOCSP-26445: use struct in watch for changes * MW PR suggestions
1 parent 882e5f2 commit e35b3fc

File tree

1 file changed

+32
-29
lines changed
  • source/fundamentals/crud/read-operations

1 file changed

+32
-29
lines changed

source/fundamentals/crud/read-operations/watch.txt

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Sample Data
2424
~~~~~~~~~~~
2525

2626
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
2828
snippet:
2929

3030
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort.go
@@ -39,13 +39,14 @@ snippet:
3939
you perform a write operation, the server implicitly creates
4040
them.
4141

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.
4445

4546
.. note::
4647

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.
4950

5051
Open a Change Stream
5152
--------------------
@@ -56,12 +57,12 @@ parameter and a pipeline parameter. To return all changes, pass in an empty Pipe
5657
Example
5758
~~~~~~~
5859

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
6061
outputs all changes:
6162

6263
.. code-block:: go
6364

64-
coll := client.Database("tea").Collection("ratings")
65+
coll := client.Database("db").Collection("courses")
6566

6667
// open a change stream with an empty pipeline parameter
6768
changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{})
@@ -70,21 +71,22 @@ outputs all changes:
7071
}
7172
defer changeStream.Close(context.TODO())
7273

73-
// iterate over the cursor to print the change stream events
74+
// iterate over the cursor to print the change-stream events
7475
for changeStream.Next(context.TODO()) {
7576
fmt.Println(changeStream.Current)
7677
}
7778

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:
8183

8284
.. code-block:: go
8385
:copyable: false
8486

8587
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]
8890

8991
Modify the Change Stream Output
9092
-------------------------------
@@ -107,12 +109,12 @@ You can use the following pipeline stages in this parameter:
107109
Example
108110
~~~~~~~
109111

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
111113
new delete operations:
112114

113115
.. code-block:: go
114116

115-
db := client.Database("tea")
117+
db := client.Database("db")
116118

117119
pipeline := bson.D{{"$match", bson.D{{"operationType", "delete"}}}}
118120
changeStream, err := db.Watch(context.TODO(), mongo.Pipeline{pipeline})
@@ -125,21 +127,20 @@ new delete operations:
125127
fmt.Println(changeStream.Current)
126128
}
127129

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:
130132

131133
.. code-block:: go
132134
:copyable: false
133135

134136
{"_id": {"_data": "..."},"operationType": "delete","clusterTime":
135-
{"$timestamp":{"t":"...","i":"..."}},"ns": {"db": "tea","coll": "ratings"},
137+
{"$timestamp":{"t":"...","i":"..."}},"ns": {"db": "db","coll": "courses"},
136138
"documentKey": {"_id": {"$oid":"..."}}}
137139

138140
.. note::
139141

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.
143144

144145
Modify the Behavior of Watch()
145146
------------------------------
@@ -162,12 +163,12 @@ For more information on these fields, visit the
162163
Example
163164
~~~~~~~
164165

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:
167168

168169
.. code-block:: go
169170

170-
coll := client.Database("tea").Collection("ratings")
171+
coll := client.Database("db").Collection("courses")
171172
opts := options.ChangeStream().SetFullDocument(options.UpdateLookup)
172173

173174
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
180181
fmt.Println(changeStream.Current)
181182
}
182183

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:
185187

186188
.. code-block:: go
187189
:copyable: false
188190

189191
{"_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"}},
193196
"removedFields": [],"truncatedArrays": []}}
194197

195198
Without specifying the ``FullDocument`` option, the same update operation no longer

0 commit comments

Comments
 (0)