@@ -16,32 +16,33 @@ Overview
1616In this guide, you can learn how to use bulk operations in the
1717MongoDB Java Driver.
1818
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.
2024
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:
2828
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.
3332
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.
3736
38- Performing Bulk Operations
39- --------------------------
37+ .. _java-sync-coll-bulk-write:
38+
39+ Collection Bulk Write
40+ ---------------------
4041
4142Bulk 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.
4546
4647The following sections show how to create and use each ``WriteModel``
4748document. The examples in each section use the following documents in the
@@ -305,8 +306,8 @@ see the following API documentation:
305306
306307.. _java-sync-client-bulk-write:
307308
308- Client Bulk Write Method
309- ------------------------
309+ Client Bulk Write
310+ -----------------
310311
311312When connecting to a deployment running {+mdb-server+} 8.0 or later,
312313you can use the ``MongoClient.bulkWrite()`` method to write
@@ -327,19 +328,19 @@ methods to represent different write operations, such as ``ClientNamespacedWrite
327328These methods take a ``MongoNamespace`` object that defines which
328329database and collection to write to, and a ``Document`` object that defines
329330information 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.
331333
332334The following sections describe how to use the client ``bulkWrite()`` method.
333335
334336Insert Example
335337~~~~~~~~~~~~~~
336338
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.
343344
344345.. code-block:: java
345346
@@ -348,18 +349,29 @@ want each write operation to be applied to.
348349
349350 List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
350351
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+ );
353365
354- ClientBulkWriteResult result = client .bulkWrite(bulkOperations);
366+ ClientBulkWriteResult result = mongoClient .bulkWrite(bulkOperations);
355367
356368.. TODO: link documentation
357369
358370Replace Example
359371~~~~~~~~~~~~~~~
360372
361373The 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.
363375
364376.. code-block:: java
365377
@@ -371,7 +383,7 @@ existing documents in the ``people`` and ``things`` collections.
371383 bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
372384 peopleNamespace,
373385 Filters.eq("_id", 1),
374- new Document("name", "Maximus Farquaad ")
386+ new Document("name", "Frederic Hilbert ")
375387 )
376388 );
377389
@@ -382,32 +394,34 @@ existing documents in the ``people`` and ``things`` collections.
382394 )
383395 );
384396
385- ClientBulkWriteResult result = client .bulkWrite(bulkOperations);
397+ ClientBulkWriteResult result = mongoClient .bulkWrite(bulkOperations);
386398
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.
391403
392404.. _java-sync-client-bulk-write-options:
393405
394406Bulk Write Options
395407~~~~~~~~~~~~~~~~~~
396408
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 .
399411
400412Order of Execution Example
401413``````````````````````````
402414
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``
405418interface 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
407420write operations.
408421
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.
411425
412426.. code-block:: java
413427
@@ -417,18 +431,18 @@ instance of ``ClientBulkWriteOptions``.
417431
418432 List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
419433
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 )));
421435
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 ")));
424438
425- bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Princess Peach ")));
439+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Wendy Zhang ")));
426440
427- ClientBulkWriteResult result = client .bulkWrite(bulkOperations, options);
441+ ClientBulkWriteResult result = mongoClient .bulkWrite(bulkOperations, options);
428442
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 .
432446
433447.. TODO: link documentation
434448
@@ -454,8 +468,8 @@ There are two ways to execute the ``bulkWrite()`` method:
454468``MongoClient.bulkWrite()``
455469~~~~~~~~~~~~~~~~~~~~~~~~~~~
456470
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
459473databases and collections at once.
460474
461475This method uses the ``ClientNamespacedWriteModel`` and its methods
@@ -465,8 +479,8 @@ This method uses the ``ClientNamespacedWriteModel`` and its methods
465479The method can also take a ``ClientBulkWriteOptions`` object to specify different
466480options for how the command is executed.
467481
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+}
470484Manual.
471485
472486.. TODO: Add API Documentation
0 commit comments