@@ -32,19 +32,35 @@ MongoDB supports the following compound operations:
3232.. tip::
3333
3434 If you need to read and write to more than one document, use
35- :manual :`transactions </core/ transactions/ >`.
35+ :ref :`transactions <golang- transactions>`.
3636
3737Sample Data
3838~~~~~~~~~~~
3939
40- Run the following snippet to load the documents into the ``tea.ratings`` collection:
40+ The examples in this guide use the following ``Course`` struct as a model for documents
41+ in the ``courses`` collection:
42+
43+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
44+ :start-after: start-course-struct
45+ :end-before: end-course-struct
46+ :language: go
47+ :dedent:
48+
49+ To run the examples in this guide, load the sample data into the
50+ ``db.courses`` collection with the following
51+ snippet:
4152
4253.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
4354 :language: go
4455 :dedent:
4556 :start-after: begin insertDocs
4657 :end-before: end insertDocs
47- .. include:: /includes/fundamentals/tea-sample-data-ending.rst
58+
59+ Each document contains a description of a university course that
60+ includes the course title and maximum enrollment, corresponding to
61+ the ``title`` and ``enrollment`` fields in each document.
62+
63+ .. include:: /includes/fundamentals/automatic-db-coll-creation.rst
4864
4965.. _golang-find-and-delete:
5066
@@ -107,30 +123,32 @@ with the following methods:
107123Example
108124```````
109125
110- The following example matches and deletes a document where the ``type``
111- is "Assam" with the ``FindOneAndDelete()`` method:
126+ The following example uses the ``FindOneAndDelete()`` method
127+ to match and delete the first document where the ``enrollment``
128+ field value is less than 20:
112129
113130.. io-code-block::
114131 :copyable: true
115132
116133 .. input::
117134 :language: go
118135
119- filter := bson.D{{"type ", "Assam" }}
136+ filter := bson.D{{"enrollment ", bson.D{{"$lt", 20}} }}
120137
121- var deletedDoc bson.D
138+ var deletedDoc Course
122139 err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
123140 if err != nil {
124141 panic(err)
125142 }
126143
127- fmt.Println(deletedDoc)
144+ res, _ := bson.MarshalExtJSON(deletedDoc, false, false)
145+ fmt.Println(string(res))
128146
129147 .. output::
130148 :language: none
131149 :visible: false
132150
133- [{_id ObjectID("...")} {type Assam} {rating 5}]
151+ {"title":"Animal Communication","enrollment":18}
134152
135153.. _golang-find-and-update:
136154
@@ -210,11 +228,11 @@ with the following methods:
210228Example
211229```````
212230
213- The following example performs the following actions in order with the
214- ``FindOneAndUpdate()`` method :
231+ The following example uses the ``FindOneAndUpdate()`` method to
232+ perform the following actions in order :
215233
216- - Matches a document where the ``type `` is "Oolong "
217- - Updates the matched document's ``rating `` to ``9 ``
234+ - Matches the first document where the ``title `` field value includes "Modern "
235+ - Updates the matched document's ``enrollment `` field value to ``32 ``
218236- Returns the updated document
219237
220238.. io-code-block::
@@ -223,23 +241,24 @@ The following example performs the following actions in order with the
223241 .. input::
224242 :language: go
225243
226- filter := bson.D{{"type ", "Oolong" }}
227- update := bson.D{{"$set", bson.D{{"rating ", 9 }}}}
244+ filter := bson.D{{"title ", bson.D{{"$regex", "Modern"}} }}
245+ update := bson.D{{"$set", bson.D{{"enrollment ", 32 }}}}
228246 opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
229-
230- var updatedDoc bson.D
247+
248+ var updatedDoc Course
231249 err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
232250 if err != nil {
233- panic(err)
251+ panic(err)
234252 }
235-
236- fmt.Println(updatedDoc)
253+
254+ res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
255+ fmt.Println(string(res))
237256
238257 .. output::
239258 :language: none
240259 :visible: false
241260
242- [{_id ObjectID("...")} {type Oolong} {rating 9}]
261+ {"title":"Early Modern Philosophy","enrollment":32}
243262
244263.. _golang-find-and-replace:
245264
@@ -311,34 +330,36 @@ with the following methods:
311330Example
312331```````
313332
314- The following example performs the following actions in order with the
315- ``FindOneAndReplace()`` method :
333+ The following example uses the ``FindOneAndReplace()`` method to
334+ perform the following actions in order :
316335
317- - Matches a document where the ``type`` is "English Breakfast"
318- - Replaces the matched document with a new document where the ``type`` is "Ceylon" and ``rating`` is ``6``
336+ - Matches the first document where the ``title`` is "Representation Theory"
337+ - Replaces the matched document with a new document where the ``title``
338+ is "Combinatorial Theory" and the ``enrollment`` is ``35``
319339
320340.. io-code-block::
321341 :copyable: true
322342
323343 .. input::
324344 :language: go
325345
326- filter := bson.D{{"type ", "English Breakfast "}}
327- replacement := bson.D{{"type", "Ceylon"}, {"rating", 6} }
328-
329- var previousDoc bson.D
346+ filter := bson.D{{"title ", "Representation Theory "}}
347+ replacement := Course{Title: "Combinatorial Theory", Enrollment: 35 }
348+
349+ var outdatedDoc Course
330350 err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
331351 if err != nil {
332- panic(err)
352+ panic(err)
333353 }
334-
335- fmt.Println(previousDoc)
354+
355+ res, _ := bson.MarshalExtJSON(outdatedDoc, false, false)
356+ fmt.Println(string(res))
336357
337358 .. output::
338359 :language: none
339360 :visible: false
340361
341- [{_id ObjectID("...")} {type English Breakfast} {rating 5}]
362+ {"title":"Representation Theory","enrollment":40}
342363
343364Additional Information
344365----------------------
0 commit comments