Skip to content

Commit ae04d7e

Browse files
DOCSP-18350 crud bulk operations (#78)
* added CRUD Bulk Ops
1 parent d39b116 commit ae04d7e

File tree

8 files changed

+623
-11
lines changed

8 files changed

+623
-11
lines changed

source/fundamentals/crud/query-document.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Overview
1818
In this guide, you can learn how to specify a query to match a subset
1919
of documents.
2020

21+
.. _query-filter-definition-go:
22+
2123
To match a subset of documents, specify a **query filter** containing
2224
your **match criteria**. Match criteria consist of the fields and
2325
values you want present in a document. A query filter contains at least

source/fundamentals/crud/write-operations.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Write Operations
99
- :doc:`/fundamentals/crud/write-operations/change-a-document`
1010
- :doc:`/fundamentals/crud/write-operations/embedded-arrays`
1111
- :doc:`/fundamentals/crud/write-operations/upsert`
12+
- :doc:`/fundamentals/crud/write-operations/bulk`
1213

1314
.. toctree::
1415
:caption: Write Operations
@@ -18,3 +19,4 @@ Write Operations
1819
/fundamentals/crud/write-operations/change-a-document
1920
/fundamentals/crud/write-operations/embedded-arrays
2021
/fundamentals/crud/write-operations/upsert
22+
/fundamentals/crud/write-operations/bulk
Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
.. _bulk_golang:
2+
3+
===============
4+
Bulk Operations
5+
===============
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Overview
16+
--------
17+
18+
In this guide, you can learn how to use **bulk operations**.
19+
20+
Bulk operations perform a large number of write operations. Instead of
21+
making a call for each operation to the database, bulk operations
22+
perform multiple operations with one call to the database.
23+
24+
Sample Data
25+
~~~~~~~~~~~
26+
27+
To run the example in this guide, load the sample data into the
28+
``ratings`` collection of the ``tea`` database with the following
29+
snippet:
30+
31+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
32+
:language: go
33+
:dedent:
34+
:start-after: begin insert docs
35+
:end-before: end insert docs
36+
37+
.. include:: /includes/fundamentals/tea-sample-data-ending.rst
38+
39+
Bulk Write
40+
----------
41+
42+
To perform a bulk operation, pass a slice of :ref:`WriteModel
43+
<write-model-go>` documents to the ``BulkWrite()`` function.
44+
45+
Modify Behavior
46+
~~~~~~~~~~~~~~~
47+
48+
The ``BulkWrite()`` function optionally takes a ``BulkWriteOptions``
49+
type, which represents options you can use to modify its behavior. If
50+
you don't specify a ``BulkWriteOptions``, the driver uses the default
51+
values for each option.
52+
53+
The ``BulkWriteOptions`` type allows you to configure options with the
54+
following functions:
55+
56+
.. list-table::
57+
:widths: 30 70
58+
:header-rows: 1
59+
60+
* - Function
61+
- Description
62+
63+
* - ``SetBypassDocumentValidation()``
64+
- | Whether to allow the write to opt-out of document level validation.
65+
| Default: ``false``
66+
67+
* - ``SetOrdered()``
68+
- | Whether to stop performing write operations after an error occurs.
69+
| Default: ``true``
70+
71+
Return Values
72+
~~~~~~~~~~~~~
73+
74+
The ``BulkWrite()`` function returns a ``BulkWriteResult`` type, which
75+
contains information about the bulk operation if it's successful. The
76+
``BulkWriteResult`` type contains the following properties:
77+
78+
.. list-table::
79+
:widths: 30 70
80+
:header-rows: 1
81+
82+
* - Property
83+
- Description
84+
85+
* - ``InsertedCount``
86+
- The number of documents inserted.
87+
88+
* - ``MatchedCount``
89+
- The number of documents matched by the :ref:`query filter <query-filter-definition-go>` in update and replace operations.
90+
91+
* - ``ModifiedCount``
92+
- The number of documents modified by update and replace operations.
93+
94+
* - ``DeletedCount``
95+
- The number of documents deleted.
96+
97+
* - ``UpsertedCount``
98+
- The number of documents :ref:`upserted <upsert-definition-go>` by update and replace operations.
99+
100+
* - ``UpsertedIDs``
101+
- A map of an operation index to the ``_id`` of each :ref:`upserted <upsert-definition-go>` document.
102+
103+
.. _write-model-go:
104+
105+
Operations
106+
----------
107+
108+
A ``WriteModel`` represents an insert, replace, update, or delete operation.
109+
110+
Insert
111+
~~~~~~
112+
113+
To perform an insert operation, create an ``InsertOneModel`` specifying
114+
the document you want to insert. To insert multiple documents, create an
115+
``InsertOneModel`` for each document you want to insert.
116+
117+
The ``InsertOneModel`` allows you to specify its behavior with the
118+
following function:
119+
120+
.. list-table::
121+
:widths: 30 70
122+
:header-rows: 1
123+
124+
* - Function
125+
- Description
126+
127+
* - ``SetDocument()``
128+
- | The document to insert.
129+
130+
Example
131+
```````
132+
133+
This following example creates two ``InsertOneModel`` instances to
134+
insert two documents:
135+
136+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
137+
:language: go
138+
:dedent:
139+
:start-after: begin bulk insert model
140+
:end-before: end bulk insert model
141+
142+
Replace
143+
~~~~~~~
144+
145+
To perform a replace operation, create a ``ReplaceOneModel`` specifying
146+
the document you want to replace and a :ref:`replacement document
147+
<replacement-document>`. To replace multiple documents, create a
148+
``ReplaceOneModel`` for each document you want to replace.
149+
150+
The ``ReplaceOneModel`` allows you to specify its behavior with the
151+
following functions:
152+
153+
.. list-table::
154+
:widths: 30 70
155+
:header-rows: 1
156+
157+
* - Function
158+
- Description
159+
160+
* - ``SetCollation()``
161+
- | The type of language collation to use when sorting results.
162+
163+
* - ``SetFilter()``
164+
- | The :ref:`query filter <query-filter-definition-go>` specifying which document to replace.
165+
166+
* - ``SetHint()``
167+
- | The index to use to scan for documents.
168+
169+
* - ``SetReplacement()``
170+
- | The document to replace the matched document with.
171+
172+
* - ``SetUpsert()``
173+
- | Whether to insert a new document if the :ref:`query filter <query-filter-definition-go>` doesn't match any documents.
174+
175+
Example
176+
```````
177+
178+
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``:
181+
182+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
183+
:language: go
184+
:dedent:
185+
:start-after: begin bulk replace model
186+
:end-before: end bulk replace model
187+
188+
Update
189+
~~~~~~
190+
191+
To perform an update operation, create an ``UpdateOneModel`` specifying
192+
the document you want to update and an :ref:`update document
193+
<update-document>`. To update multiple documents, use the
194+
``UpdateManyModel``.
195+
196+
The ``UpdateOneModel`` and ``UpdateManyModel`` allow you to specify
197+
their behavior with the following functions:
198+
199+
.. list-table::
200+
:widths: 30 70
201+
:header-rows: 1
202+
203+
* - Function
204+
- Description
205+
206+
* - ``SetArrayFilters()``
207+
- | The array elements the update applies to.
208+
209+
* - ``SetCollation()``
210+
- | The type of language collation to use when sorting results.
211+
212+
* - ``SetFilter()``
213+
- | The :ref:`query filter <query-filter-definition-go>` specifying which document to update.
214+
215+
* - ``SetHint()``
216+
- | The index to use to scan for documents.
217+
218+
* - ``SetUpdate()``
219+
- | The modifications to apply on the matched documents.
220+
221+
* - ``SetUpsert()``
222+
- | Whether to insert a new document if the :ref:`query filter <query-filter-definition-go>` doesn't match any documents.
223+
224+
Example
225+
```````
226+
227+
The following example creates an ``UpdateOneModel`` to decrement a
228+
documents ``rating`` by ``2`` if their ``type`` is "Masala":
229+
230+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
231+
:language: go
232+
:dedent:
233+
:start-after: begin bulk update model
234+
:end-before: end bulk update model
235+
236+
Delete
237+
~~~~~~
238+
239+
To perform a delete operation, create a ``DeleteOneModel`` specifying
240+
the document you want to delete. To delete multiple documents, use the
241+
``DeleteManyModel``.
242+
243+
The ``DeleteOneModel`` and ``DeleteManyModel`` allow you to specify
244+
their behavior with the following functions:
245+
246+
.. list-table::
247+
:widths: 30 70
248+
:header-rows: 1
249+
250+
* - Function
251+
- Description
252+
253+
* - ``SetCollation()``
254+
- | The type of language collation to use when sorting results.
255+
256+
* - ``SetFilter()``
257+
- | The :ref:`query filter <query-filter-definition-go>` specifying which document to delete.
258+
259+
* - ``SetHint()``
260+
- | The index to use to scan for documents.
261+
262+
Example
263+
```````
264+
265+
The following example creates a ``DeleteManyModel`` to delete
266+
documents where the ``rating`` is greater than ``7``:
267+
268+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
269+
:language: go
270+
:dedent:
271+
:start-after: begin bulk delete model
272+
:end-before: end bulk delete model
273+
274+
Execution Order
275+
---------------
276+
277+
The ``BulkWrite()`` function allows you to specify if you want to
278+
execute the bulk operations as ordered or unordered in its
279+
``BulkWriteOptions``.
280+
281+
Ordered
282+
~~~~~~~
283+
284+
By default, the ``BulkWrite()`` function executes bulk operations in
285+
order you added them and stops if an error occurs.
286+
287+
.. tip::
288+
289+
This is equivalent to specifying ``true`` in the ``SetOrdered()``
290+
function:
291+
292+
.. code-block:: go
293+
294+
opts := options.BulkWrite().SetOrdered(true)
295+
296+
Unordered
297+
~~~~~~~~~
298+
299+
To execute bulk write operations in any order and continue if an error
300+
occurs, specify ``false`` to the ``SetOrdered()`` function. The function
301+
reports the errors afterwards.
302+
303+
Example
304+
```````
305+
306+
The following example performs the following actions in any order:
307+
308+
- 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``.
312+
313+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/bulkOps.go
314+
:language: go
315+
:dedent:
316+
:start-after: begin unordered
317+
:end-before: end unordered
318+
319+
After running this example, the output resembles the following:
320+
321+
.. code-block:: none
322+
:copyable: false
323+
324+
Number of documents inserted: 2
325+
Number of documents replaced or updated: 3
326+
Number of documents deleted: 2
327+
328+
The following documents are present in the ``ratings`` collection after
329+
the bulk operation:
330+
331+
.. code-block:: none
332+
:copyable: false
333+
334+
[{_id ObjectID("...")} {type Masala} {rating 10}]
335+
[{_id ObjectID("...")} {type Matcha} {rating 7}]
336+
337+
Additional Information
338+
----------------------
339+
340+
For a runnable example on performing a bulk operation, see the
341+
:ref:`<bulk-ops-usage-example-go>` usage example.
342+
343+
Related Operations
344+
~~~~~~~~~~~~~~~~~~
345+
346+
For more information on performing the operations mentioned, see the
347+
following guides:
348+
349+
- :ref:`<query_document_golang>`
350+
- :ref:`<insert_guide_golang>`
351+
- :ref:`<change_document_golang>`
352+
- :ref:`<delete_guide_golang>`
353+
- :manual:`Bulk Write Operations </core/bulk-write-operations/>`
354+
355+
API Documentation
356+
~~~~~~~~~~~~~~~~~
357+
358+
For more information on any of the functions or types discussed in this
359+
guide, see the following API Documentation:
360+
361+
- `BulkWrite() <{+api+}/mongo#Collection.BulkWrite>`__
362+
- `BulkWriteOptions <{+api+}/mongo/options#BulkWriteOptions>`__
363+
- `BulkWriteResult <{+api+}/mongo#BulkWriteResult>`__
364+
- `NewInsertOneModel() <{+api+}/mongo#NewUpdateOneModel>`__
365+
- `NewReplaceOneModel() <{+api+}/mongo#NewReplaceOneModel>`__
366+
- `NewReplaceOneModel() <{+api+}/mongo#NewReplaceOneModel>`__
367+
- `NewUpdateOneModel() <{+api+}/mongo#NewUpdateOneModel>`__
368+
- `NewUpdateManyModel() <{+api+}/mongo#NewReplaceOneModel>`__
369+
- `NewDeleteOneModel() <{+api+}/mongo#NewReplaceOneModel>`__
370+
- `NewDeleteManyModel() <{+api+}/mongo#NewReplaceOneModel>`__

0 commit comments

Comments
 (0)