@@ -16,32 +16,33 @@ Overview
16
16
In this guide, you can learn how to use bulk operations in the
17
17
MongoDB Java Driver.
18
18
19
- .. note:: Client Bulk Write Method
19
+ To perform a single create, replace, update, or delete operation, you can use
20
+ the corresponding method. For example, to insert one document and replace one
21
+ document, you can use the ``insertOne()`` and ``replaceOne()`` methods. The
22
+ ``MongoClient`` makes a call to the database for each operation. You can reduce
23
+ the number of calls to one by using bulk write operations.
20
24
21
- This guide primarily focuses on the ``MongoCollection.bulkWrite()``
22
- method, which allows you to perform bulk operations on a collection.
23
- When connecting to a deployment running {+mdb-server+} 8.0 or later,
24
- you can use the ``MongoClient.bulkWrite()`` method to write
25
- to multiple databases and collections in the same call. See the
26
- :ref:`java-sync-client-bulk-write` section below to learn how to use the
27
- ``MongoClient.bulkWrite()`` method.
25
+ You can perform bulk write operations by using the bulk write API in the
26
+ {+driver-short+} driver to perform multiple data changes in one call. You can
27
+ perform bulk operations at the following levels:
28
28
29
- To perform a create, replace, update, or delete operation,
30
- use its corresponding method. For example, to insert one document,
31
- update multiple documents, and delete one document in your collection,
32
- use the ``insertOne()``, ``updateMany()`` and ``deleteOne()`` methods.
29
+ - :ref:`Collection Level <java-sync-coll-bulk-write>`: You can use the
30
+ ``MongoCollection.bulkWrite()`` API to perform bulk write operations on a
31
+ collection.
33
32
34
- The ``MongoClient`` performs these operations by making a call for each
35
- operation to the database. You can reduce the number of calls to the
36
- database to one by using bulk operations.
33
+ - :ref:`Client Level <java-sync-client-bulk-write>`: In {+mdb-server+} version
34
+ 8.0 or later, you can use the ``MongoClient.bulkWrite()`` API to perform bulk write
35
+ operations on multiple collections and databases in the same cluster.
37
36
38
- Performing Bulk Operations
39
- --------------------------
37
+ .. _java-sync-coll-bulk-write:
38
+
39
+ Collection Bulk Write
40
+ ---------------------
40
41
41
42
Bulk operations consist of a large number of write operations. To perform
42
- a bulk operation, pass a ``List`` of ``WriteModel`` documents to the
43
- `` bulkWrite()`` method. A ``WriteModel`` is a model that represents any
44
- of the write operations.
43
+ a bulk operation at the collection level , pass a ``List`` of ``WriteModel``
44
+ documents to the ``MongoCollection. bulkWrite()`` method. A ``WriteModel`` is a model that
45
+ represents any of the write operations.
45
46
46
47
The following sections show how to create and use each ``WriteModel``
47
48
document. The examples in each section use the following documents in the
@@ -305,8 +306,8 @@ see the following API documentation:
305
306
306
307
.. _java-sync-client-bulk-write:
307
308
308
- Client Bulk Write Method
309
- ------------------------
309
+ Client Bulk Write
310
+ -----------------
310
311
311
312
When connecting to a deployment running {+mdb-server+} 8.0 or later,
312
313
you can use the ``MongoClient.bulkWrite()`` method to write
@@ -327,19 +328,19 @@ methods to represent different write operations, such as ``ClientNamespacedWrite
327
328
These methods take a ``MongoNamespace`` object that defines which
328
329
database and collection to write to, and a ``Document`` object that defines
329
330
information about the write operation. Some methods, such as ``updateOne()`` and
330
- ``replaceOne()``, also take a ``Filters`` object that defines the filter for the operation.
331
+ ``replaceOne()``, also take a ``Filters`` object that defines the subset of
332
+ documents to be updated or replaced.
331
333
332
334
The following sections describe how to use the client ``bulkWrite()`` method.
333
335
334
336
Insert Example
335
337
~~~~~~~~~~~~~~
336
338
337
- This following example shows how to use the ``bulkWrite()`` method to insert
338
- two documents. One document is inserted into the ``people`` collection in a
339
- database named ``db``. The other document is inserted into a collection called ``things``
340
- in the ``db`` database.
341
- We use the ``MongoNamespace`` object to define the database and collection we
342
- want each write operation to be applied to.
339
+ This example shows how to use the ``bulkWrite()`` method to insert
340
+ two documents. One document is inserted into the ``db.people`` collection, while
341
+ the other document is inserted into the ``db.things`` collection.
342
+ The ``MongoNamespace`` instance defines the databases and collections that
343
+ each write operation applies to.
343
344
344
345
.. code-block:: java
345
346
@@ -348,18 +349,29 @@ want each write operation to be applied to.
348
349
349
350
List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
350
351
351
- bulkOperations.add(ClientNamespacedWriteModel.insertOne(peopleNamespace, new Document("name", "Julia Smith")));
352
- bulkOperations.add(ClientNamespacedWriteModel.insertOne(thingsNamespace, new Document("object", "washing machine")));
352
+ bulkOperations.add(
353
+ ClientNamespacedWriteModel.insertOne(
354
+ peopleNamespace,
355
+ new Document("name", "Julia Smith")
356
+ )
357
+ );
358
+
359
+ bulkOperations.add(
360
+ ClientNamespacedWriteModel.insertOne(
361
+ thingsNamespace,
362
+ new Document("object", "washing machine")
363
+ )
364
+ );
353
365
354
- ClientBulkWriteResult result = client .bulkWrite(bulkOperations);
366
+ ClientBulkWriteResult result = mongoClient .bulkWrite(bulkOperations);
355
367
356
368
.. TODO: link documentation
357
369
358
370
Replace Example
359
371
~~~~~~~~~~~~~~~
360
372
361
373
The following example shows how to use the ``bulkWrite()`` method to replace
362
- existing documents in the ``people`` and ``things`` collections.
374
+ existing documents in the ``db. people`` and ``db. things`` collections.
363
375
364
376
.. code-block:: java
365
377
@@ -371,7 +383,7 @@ existing documents in the ``people`` and ``things`` collections.
371
383
bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
372
384
peopleNamespace,
373
385
Filters.eq("_id", 1),
374
- new Document("name", "Maximus Farquaad ")
386
+ new Document("name", "Frederic Hilbert ")
375
387
)
376
388
);
377
389
@@ -382,32 +394,34 @@ existing documents in the ``people`` and ``things`` collections.
382
394
)
383
395
);
384
396
385
- ClientBulkWriteResult result = client .bulkWrite(bulkOperations);
397
+ ClientBulkWriteResult result = mongoClient .bulkWrite(bulkOperations);
386
398
387
- After this example runs successfully, the document in the ``people `` collection in
388
- which the ``_id `` field has the value of ``1`` is replaced with a new
389
- document. The document in the ``things`` collection in which the
390
- ``_id`` field has the value of ``1`` is replaced with a new document.
399
+ After this example runs successfully, the document that has an ``_id `` value of ``1``
400
+ in the ``people `` collection is replaced with a new document. The document in
401
+ the ``things`` collection that has an ``_id`` value of ``1``
402
+ is replaced with a new document.
391
403
392
404
.. _java-sync-client-bulk-write-options:
393
405
394
406
Bulk Write Options
395
407
~~~~~~~~~~~~~~~~~~
396
408
397
- You can use the ``ClientBulkWriteOptions`` interface to specify options when
398
- running the ``bulkWrite()`` method .
409
+ You can pass an instance of ``ClientBulkWriteOptions`` to the ``bulkWrite()``
410
+ method to specify options when running the bulk write operations .
399
411
400
412
Order of Execution Example
401
413
``````````````````````````
402
414
403
- By default, the write operations execute in the order that you specify them. However,
404
- you can pass ``false`` to the ``ordered()`` method on the ``ClientBulkWriteOptions``
415
+ By default, the driver performs write operations in the order that you specify them until
416
+ an error occurs, or until they execute successfully. However, you can pass
417
+ ``false`` to the ``ordered()`` method on the ``ClientBulkWriteOptions``
405
418
interface to perform write operations in an unordered way. When using the unordered
406
- option, an error-producing operation will not block execution of other bulk
419
+ option, an error-producing operation does not block execution of other bulk
407
420
write operations.
408
421
409
- The following code shows how to set the ``ordered()`` method when creating an
410
- instance of ``ClientBulkWriteOptions``.
422
+ The following code sets the ``ordered()`` method on an
423
+ instance of ``ClientBulkWriteOptions`` and performs a bulk write operation to
424
+ insert multiple documents.
411
425
412
426
.. code-block:: java
413
427
@@ -417,18 +431,18 @@ instance of ``ClientBulkWriteOptions``.
417
431
418
432
List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
419
433
420
- bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("_id", 1).append("name", "Donkey Kong )));
434
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("_id", 1).append("name", "Rudra Suraj )));
421
435
422
- // duplicate key
423
- bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("_id", 1).append("name", "Mario")));
436
+ // Causes a duplicate key error
437
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("_id", 1).append("name", "Mario Bianchi ")));
424
438
425
- bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Princess Peach ")));
439
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Wendy Zhang ")));
426
440
427
- ClientBulkWriteResult result = client .bulkWrite(bulkOperations, options);
441
+ ClientBulkWriteResult result = mongoClient .bulkWrite(bulkOperations, options);
428
442
429
- Even though the write operation inserting a document with a duplicate key will
430
- result in an error, the other operations will be executed because we have specified
431
- the ``ordered()`` option to be ``false`` .
443
+ Even though the write operation inserting a document with a duplicate key results
444
+ in an error, the other operations are executed because the write operation is
445
+ unordered .
432
446
433
447
.. TODO: link documentation
434
448
@@ -454,8 +468,8 @@ There are two ways to execute the ``bulkWrite()`` method:
454
468
``MongoClient.bulkWrite()``
455
469
~~~~~~~~~~~~~~~~~~~~~~~~~~~
456
470
457
- In {+mdb-server+} version 8.0 and {+driver-short+} version 5.3 and later, you
458
- can use the ``bulkWrite()`` method to perform bulk operations on multiple
471
+ When connecting to a deployment running {+mdb-server+} version 8.0 or later, you
472
+ can use the ``MongoClient. bulkWrite()`` method to perform bulk operations on multiple
459
473
databases and collections at once.
460
474
461
475
This method uses the ``ClientNamespacedWriteModel`` and its methods
@@ -465,8 +479,8 @@ This method uses the ``ClientNamespacedWriteModel`` and its methods
465
479
The method can also take a ``ClientBulkWriteOptions`` object to specify different
466
480
options for how the command is executed.
467
481
468
- To learn more about the client ``bulkWrite`` command, take a look at the
469
- :manual:`bulkWrite <reference/command/bulkWrite/>` guide in the {+mdb-server+}
482
+ To learn more about the client ``bulkWrite`` command, see the
483
+ :manual:`bulkWrite() <reference/command/bulkWrite/>` method reference in the {+mdb-server+}
470
484
Manual.
471
485
472
486
.. TODO: Add API Documentation
0 commit comments