@@ -32,19 +32,35 @@ MongoDB supports the following compound operations:
32
32
.. tip::
33
33
34
34
If you need to read and write to more than one document, use
35
- :manual :`transactions </core/ transactions/ >`.
35
+ :ref :`transactions <golang- transactions>`.
36
36
37
37
Sample Data
38
38
~~~~~~~~~~~
39
39
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:
41
52
42
53
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
43
54
:language: go
44
55
:dedent:
45
56
:start-after: begin insertDocs
46
57
: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
48
64
49
65
.. _golang-find-and-delete:
50
66
@@ -107,30 +123,32 @@ with the following methods:
107
123
Example
108
124
```````
109
125
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:
112
129
113
130
.. io-code-block::
114
131
:copyable: true
115
132
116
133
.. input::
117
134
:language: go
118
135
119
- filter := bson.D{{"type ", "Assam" }}
136
+ filter := bson.D{{"enrollment ", bson.D{{"$lt", 20}} }}
120
137
121
- var deletedDoc bson.D
138
+ var deletedDoc Course
122
139
err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
123
140
if err != nil {
124
141
panic(err)
125
142
}
126
143
127
- fmt.Println(deletedDoc)
144
+ res, _ := bson.MarshalExtJSON(deletedDoc, false, false)
145
+ fmt.Println(string(res))
128
146
129
147
.. output::
130
148
:language: none
131
149
:visible: false
132
150
133
- [{_id ObjectID("...")} {type Assam} {rating 5}]
151
+ {"title":"Animal Communication","enrollment":18}
134
152
135
153
.. _golang-find-and-update:
136
154
@@ -210,11 +228,11 @@ with the following methods:
210
228
Example
211
229
```````
212
230
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 :
215
233
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 ``
218
236
- Returns the updated document
219
237
220
238
.. io-code-block::
@@ -223,23 +241,24 @@ The following example performs the following actions in order with the
223
241
.. input::
224
242
:language: go
225
243
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 }}}}
228
246
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
229
-
230
- var updatedDoc bson.D
247
+
248
+ var updatedDoc Course
231
249
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
232
250
if err != nil {
233
- panic(err)
251
+ panic(err)
234
252
}
235
-
236
- fmt.Println(updatedDoc)
253
+
254
+ res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
255
+ fmt.Println(string(res))
237
256
238
257
.. output::
239
258
:language: none
240
259
:visible: false
241
260
242
- [{_id ObjectID("...")} {type Oolong} {rating 9}]
261
+ {"title":"Early Modern Philosophy","enrollment":32}
243
262
244
263
.. _golang-find-and-replace:
245
264
@@ -311,34 +330,36 @@ with the following methods:
311
330
Example
312
331
```````
313
332
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 :
316
335
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``
319
339
320
340
.. io-code-block::
321
341
:copyable: true
322
342
323
343
.. input::
324
344
:language: go
325
345
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
330
350
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
331
351
if err != nil {
332
- panic(err)
352
+ panic(err)
333
353
}
334
-
335
- fmt.Println(previousDoc)
354
+
355
+ res, _ := bson.MarshalExtJSON(outdatedDoc, false, false)
356
+ fmt.Println(string(res))
336
357
337
358
.. output::
338
359
:language: none
339
360
:visible: false
340
361
341
- [{_id ObjectID("...")} {type English Breakfast} {rating 5}]
362
+ {"title":"Representation Theory","enrollment":40}
342
363
343
364
Additional Information
344
365
----------------------
0 commit comments