@@ -24,23 +24,35 @@ perform multiple operations with one call to the database.
2424Sample Data
2525~~~~~~~~~~~
2626
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:
3029
3130.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
31+ :start-after: start-book-struct
32+ :end-before: end-book-struct
3233 :language: go
3334 :dedent:
34- :start-after: begin insert docs
35- :end-before: end insert docs
3635
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
3850
3951Bulk Write
4052----------
4153
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.
4456
4557Modify Behavior
4658~~~~~~~~~~~~~~~
@@ -176,8 +188,7 @@ Example
176188```````
177189
178190The 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:
181192
182193.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
183194 :language: go
@@ -225,7 +236,7 @@ Example
225236```````
226237
227238The 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 ":
229240
230241.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
231242 :language: go
@@ -263,7 +274,7 @@ Example
263274```````
264275
265276The 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 ``:
267278
268279.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
269280 :language: go
@@ -306,9 +317,10 @@ Example
306317The following example performs the following actions in any order:
307318
308319- 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".
312324
313325.. io-code-block::
314326 :copyable: true
@@ -317,16 +329,16 @@ The following example performs the following actions in any order:
317329 :language: go
318330
319331 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"}} }}),
327339 }
328340 opts := options.BulkWrite().SetOrdered(false)
329-
341+
330342 results, err := coll.BulkWrite(context.TODO(), models, opts)
331343 if err != nil {
332344 panic(err)
@@ -341,17 +353,18 @@ The following example performs the following actions in any order:
341353 :visible: false
342354
343355 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
346358
347359The following documents are present in the ``ratings`` collection after
348360the bulk operation:
349361
350362.. code-block:: none
351363 :copyable: false
352364
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}
355368
356369Additional Information
357370----------------------
0 commit comments