@@ -24,23 +24,35 @@ perform multiple operations with one call to the database.
24
24
Sample Data
25
25
~~~~~~~~~~~
26
26
27
- To run the example in this guide, load the sample data into the
28
- ``tea.ratings`` collection with the following
29
- snippet:
27
+ The examples in this guide use the following ``Book`` struct as a model for documents
28
+ in the ``books`` collection:
30
29
31
30
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
31
+ :start-after: start-book-struct
32
+ :end-before: end-book-struct
32
33
:language: go
33
34
:dedent:
34
- :start-after: begin insert docs
35
- :end-before: end insert docs
36
35
37
- .. include:: /includes/fundamentals/tea-sample-data-ending.rst
36
+ To run the examples in this guide, load the sample data into the
37
+ ``db.books`` collection with the following snippet:
38
+
39
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
40
+ :language: go
41
+ :dedent:
42
+ :start-after: begin insertDocs
43
+ :end-before: end insertDocs
44
+
45
+ Each document contains a description of a book that
46
+ includes the title, author, and page length corresponding to
47
+ the ``title``, ``author``, and ``length`` fields in each document.
48
+
49
+ .. include:: /includes/fundamentals/automatic-db-coll-creation.rst
38
50
39
51
Bulk Write
40
52
----------
41
53
42
- To perform a bulk operation, pass a slice of :ref:`WriteModel
43
- <golang-write-model>` documents to the ``BulkWrite()`` method.
54
+ To perform a bulk operation, pass an array of :ref:`WriteModel
55
+ <golang-write-model>` documents to the ``BulkWrite()`` method.
44
56
45
57
Modify Behavior
46
58
~~~~~~~~~~~~~~~
@@ -176,8 +188,7 @@ Example
176
188
```````
177
189
178
190
The following example creates a ``ReplaceOneModel`` to replace a
179
- document where the ``type`` is "Earl Grey" with a document where the
180
- ``type`` is "Matcha" and the ``rating`` is ``8``:
191
+ document where the ``title`` is "Lucy" with a new document:
181
192
182
193
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
183
194
:language: go
@@ -225,7 +236,7 @@ Example
225
236
```````
226
237
227
238
The following example creates an ``UpdateOneModel`` to decrement a
228
- documents ``rating `` by ``2 `` if their ``type `` is "Masala ":
239
+ document's ``length `` by ``15 `` if the ``author `` is "Elena Ferrante ":
229
240
230
241
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
231
242
:language: go
@@ -263,7 +274,7 @@ Example
263
274
```````
264
275
265
276
The following example creates a ``DeleteManyModel`` to delete
266
- documents where the ``rating `` is greater than ``7 ``:
277
+ documents where the ``length `` is greater than ``300 ``:
267
278
268
279
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
269
280
:language: go
@@ -306,9 +317,10 @@ Example
306
317
The following example performs the following actions in any order:
307
318
308
319
- Inserts two documents.
309
- - Replaces a document where the type is "Earl Grey" with a new document.
310
- - Increments all documents ``rating`` by ``3`` if their current rating is less than ``7``.
311
- - Deletes all documents where the rating is ``9``.
320
+ - Replaces a document where the ``title`` is "My Brilliant Friend" with a new document.
321
+ - Increments every document's ``length`` by ``10`` if the current
322
+ ``length`` value is less than ``200``.
323
+ - Deletes all documents where the ``author`` field value includes "Jam".
312
324
313
325
.. io-code-block::
314
326
:copyable: true
@@ -317,16 +329,16 @@ The following example performs the following actions in any order:
317
329
:language: go
318
330
319
331
models := []mongo.WriteModel{
320
- mongo.NewInsertOneModel().SetDocument(bson.D{{"type ", "Oolong"}, {"rating", 9} }),
321
- mongo.NewInsertOneModel().SetDocument(bson.D{{"type ", "Assam"}, {"rating", 6} }),
322
- mongo.NewReplaceOneModel().SetFilter(bson.D{{"type ", "Earl Grey "}}).
323
- SetReplacement(bson.D{{"type ", "Matcha"}, {"rating", 4} }),
324
- mongo.NewUpdateManyModel().SetFilter(bson.D{{"rating ", bson.D{{"$lt", 7 }}}}).
325
- SetUpdate(bson.D{{"$inc", bson.D{{"rating ", 3 }}}}),
326
- mongo.NewDeleteManyModel().SetFilter(bson.D{{"rating ", 9 }}),
332
+ mongo.NewInsertOneModel().SetDocument(Book{Title: "Middlemarch ", Author: "George Eliot", Length: 904 }),
333
+ mongo.NewInsertOneModel().SetDocument(Book{Title: "Pale Fire ", Author: "Vladimir Nabokov", Length: 246 }),
334
+ mongo.NewReplaceOneModel().SetFilter(bson.D{{"title ", "My Brilliant Friend "}}).
335
+ SetReplacement(Book{Title: "Atonement ", Author: "Ian McEwan", Length: 351 }),
336
+ mongo.NewUpdateManyModel().SetFilter(bson.D{{"length ", bson.D{{"$lt", 200 }}}}).
337
+ SetUpdate(bson.D{{"$inc", bson.D{{"length ", 10 }}}}),
338
+ mongo.NewDeleteManyModel().SetFilter(bson.D{{"author ", bson.D{{"$regex", "Jam"}} }}),
327
339
}
328
340
opts := options.BulkWrite().SetOrdered(false)
329
-
341
+
330
342
results, err := coll.BulkWrite(context.TODO(), models, opts)
331
343
if err != nil {
332
344
panic(err)
@@ -341,17 +353,18 @@ The following example performs the following actions in any order:
341
353
:visible: false
342
354
343
355
Number of documents inserted: 2
344
- Number of documents replaced or updated: 3
345
- Number of documents deleted: 2
356
+ Number of documents replaced or updated: 2
357
+ Number of documents deleted: 1
346
358
347
359
The following documents are present in the ``ratings`` collection after
348
360
the bulk operation:
349
361
350
362
.. code-block:: none
351
363
:copyable: false
352
364
353
- [{_id ObjectID("...")} {type Masala} {rating 10}]
354
- [{_id ObjectID("...")} {type Matcha} {rating 7}]
365
+ {"title":"Atonement","author":"Ian McEwan","length":351}
366
+ {"title":"Middlemarch","author":"George Eliot","length":904}
367
+ {"title":"Pale Fire","author":"Vladimir Nabokov","length":246}
355
368
356
369
Additional Information
357
370
----------------------
0 commit comments