Skip to content

Commit 406b19e

Browse files
authored
DOCSP-26447: use struct in write ops (mongodb#224)
* DOCSP-26447: use struct in write ops -- BULK OPS pg * DELETE pg + fixes * ARRAY PG + fixes * INSERT PG + fixes * UPSERT PG * JS PR fixes 1
1 parent 0471cd4 commit 406b19e

File tree

11 files changed

+430
-274
lines changed

11 files changed

+430
-274
lines changed

source/fundamentals/crud/write-operations.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Write Operations
1010
- :ref:`golang-delete-guide`
1111
- :ref:`golang-change-document`
1212
- :ref:`golang-update-arrays`
13-
- :ref:`golang-upsert`
13+
- :ref:`golang-upsert-guide`
1414
- :ref:`golang-bulk`
1515

1616
.. toctree::

source/fundamentals/crud/write-operations/bulk.txt

+40-27
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,35 @@ perform multiple operations with one call to the database.
2424
Sample 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

3951
Bulk 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

4557
Modify Behavior
4658
~~~~~~~~~~~~~~~
@@ -176,8 +188,7 @@ Example
176188
```````
177189

178190
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:
181192

182193
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
183194
:language: go
@@ -225,7 +236,7 @@ Example
225236
```````
226237

227238
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":
229240

230241
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
231242
:language: go
@@ -263,7 +274,7 @@ Example
263274
```````
264275

265276
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``:
267278

268279
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
269280
:language: go
@@ -306,9 +317,10 @@ Example
306317
The 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

347359
The following documents are present in the ``ratings`` collection after
348360
the 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

356369
Additional Information
357370
----------------------

source/fundamentals/crud/write-operations/delete.txt

+21-11
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,29 @@ collections using delete operations.
2121
Sample Data
2222
~~~~~~~~~~~
2323

24-
To run the example in this guide, load these documents into the
25-
``tea.ratings`` collection with the following
26-
snippet:
24+
The examples in this guide use the following ``Book`` struct as a model for documents
25+
in the ``books`` collection:
26+
27+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go
28+
:start-after: start-book-struct
29+
:end-before: end-book-struct
30+
:language: go
31+
:dedent:
32+
33+
To run the examples in this guide, load the sample data into the
34+
``db.books`` collection with the following snippet:
2735

2836
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go
2937
:language: go
3038
:dedent:
3139
:start-after: begin insertDocs
3240
:end-before: end insertDocs
3341

34-
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
42+
Each document contains a description of a book that
43+
includes the title, author, and page length corresponding to
44+
the ``title``, ``author``, and ``length`` fields in each document.
3545

36-
Each document contains a rating for a type of tea, which corresponds to
37-
the ``type`` and ``rating`` fields.
46+
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
3847

3948
Delete Operations
4049
-----------------
@@ -94,16 +103,16 @@ Example
94103
The following example performs the following with the ``DeleteMany()``
95104
method:
96105

97-
- Matches and deletes documents where the ``rating`` is greater than ``8``
98-
- Specifies the method to use the ``_id`` as the index
106+
- Matches and deletes documents where the ``length`` is greater than ``300``
107+
- Instructs the method to use the ``_id`` as the index
99108

100109
.. io-code-block::
101110
:copyable: true
102111

103112
.. input::
104113
:language: go
105114

106-
filter := bson.D{{"rating", bson.D{{"$gt", 8}}}}
115+
filter := bson.D{{"length", bson.D{{"$gt", 300}}}}
107116
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
108117

109118
result, err := coll.DeleteMany(context.TODO(), filter, opts)
@@ -141,8 +150,9 @@ following guides:
141150

142151
To learn about how the driver uses Context, see :ref:`golang-context`.
143152

144-
.. To learn more about specifying hints, see <TODO: Indexes>.
145-
.. To learn more about collations, see <TODO: Collations>.
153+
To learn more about specifying hints, see :ref:`golang-indexes`.
154+
155+
To learn more about collations, see :ref:`golang-collations`.
146156

147157
API Documentation
148158
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)