@@ -10,6 +10,9 @@ Bulk Operations
10
10
:depth: 2
11
11
:class: singlecol
12
12
13
+ .. meta::
14
+ :keywords: insert, update, replace, code example, efficiency
15
+
13
16
Overview
14
17
--------
15
18
@@ -19,28 +22,37 @@ MongoDB Java Driver.
19
22
To perform a single create, replace, update, or delete operation, you can use
20
23
the corresponding method. For example, to insert one document and replace one
21
24
document, you can use the ``insertOne()`` and ``replaceOne()`` methods. In this
22
- case, the ``MongoClient`` makes a call to the database for each operation. However,
23
- by using bulk write operations, you can reduce the number of calls.
25
+ case, the ``MongoClient`` makes a call to the database for each operation.
24
26
25
- You can perform bulk write operations by using the bulk write API in the
26
- {+driver-short+} to define multiple data changes in one method call. You can
27
- perform bulk operations at the following levels:
27
+ Generally, you can reduce the number of calls to the database by using bulk write
28
+ operations. You can perform bulk write operations at the following levels:
28
29
29
30
- :ref:`Collection Level <java-sync-coll-bulk-write>`: You can use the
30
31
``MongoCollection.bulkWrite()`` method to perform bulk write operations on a
31
- single collection.
32
+ single collection. This method groups write operations together by the kind
33
+ of operation. For example, ``MongoCollection.bulkWrite()`` puts multiple insert
34
+ operations, including ``insertOne()`` and ``insertMany()``, in the same call,
35
+ but will make two calls to the database for an insert operation and a replace
36
+ operation.
32
37
33
38
- :ref:`Client Level <java-sync-client-bulk-write>`: When running {+mdb-server+}
34
39
version 8.0 or later, you can use the ``MongoClient.bulkWrite()`` method to perform
35
40
bulk write operations on multiple collections and databases in the same cluster.
41
+ This method groups multiple kinds of write operations into one call.
36
42
37
43
.. _java-sync-coll-bulk-write:
38
44
39
45
Collection Bulk Write
40
46
---------------------
41
47
42
- Bulk operations consist of a large number of write operations. To perform
43
- a bulk operation at the collection level, pass a ``List`` of ``WriteModel``
48
+ Bulk operations consist of a large number of write operations. The
49
+ ``MongoCollection.bulkWrite()`` method splits operations of different kinds into
50
+ different batches. For example, when the method is passed an ``insertOne()``,
51
+ ``insertMany()``, and ``replaceOne()`` operation, it will split the insert operations
52
+ into one batch and the replace operation into another. During this process,
53
+ the client may reorder operations for efficiency.
54
+
55
+ To perform a bulk operation at the collection level, pass a ``List`` of ``WriteModel``
44
56
documents to the ``MongoCollection.bulkWrite()`` method. A ``WriteModel`` is a model that
45
57
represents any of the write operations.
46
58
@@ -221,14 +233,14 @@ see the following API documentation:
221
233
.. _orderOfExecution:
222
234
223
235
Order of Execution
224
- ------------------
236
+ ~~~~~~~~~~~~~~~~~~
225
237
226
238
The ``bulkWrite()`` method accepts an optional ``BulkWriteOptions`` as a
227
239
second parameter to specify whether the execution of the bulk operations is
228
- ordered or unordered.
240
+ ordered or unordered.
229
241
230
242
Ordered Execution
231
- ~~~~~~~~~~~~~~~~~
243
+ +++++++++++++++++
232
244
233
245
By default, the ``bulkWrite()`` method executes bulk operations in
234
246
order. This means that the bulk operations execute in the order you
@@ -264,7 +276,7 @@ document:
264
276
{ "_id": 6, "name": "Zaynab Hassan", "age": 37 }
265
277
266
278
Unordered Execution
267
- ~~~~~~~~~~~~~~~~~~~
279
+ +++++++++++++++++++
268
280
269
281
You can also execute bulk operations in any order by specifying "false"
270
282
to the ``order()`` method on ``BulkWriteOptions``. This means that
@@ -311,7 +323,8 @@ Client Bulk Write
311
323
312
324
When connecting to a deployment running {+mdb-server+} 8.0 or later,
313
325
you can use the ``MongoClient.bulkWrite()`` method to write
314
- to multiple databases and collections in the same cluster.
326
+ to multiple databases and collections in the same cluster. The ``MongoClient.bulkWrite()``
327
+ method does not split different kinds of operations into different batches.
315
328
316
329
In the examples in preceding sections of this page, the ``MongoCollection.bulkWrite()`` method
317
330
takes a list of ``WriteModel`` instances in which the specified subclass of
0 commit comments