Skip to content

Commit 09d5786

Browse files
committed
Initial full pass
1 parent 72b9e73 commit 09d5786

File tree

2 files changed

+117
-46
lines changed

2 files changed

+117
-46
lines changed

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

Lines changed: 106 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ MongoDB Java Driver.
1818

1919
.. note:: Improved Bulk Write Commands
2020

21-
In {+driver-short+} version 5.3 and later, you can use the
22-
``ClientNamespacedWriteModel`` object to represent the write
23-
operations. See the :ref:`improved-bulk-write` section below
24-
to see how to use this object.
25-
26-
<TODO: REWORD THis to focus on the bulkWrite function>
21+
The first half of this guide focuses on the ``MongoCollection.bulkWrite()``
22+
method. In {+mdb-server+} version 8.0 and {+driver-short+} version 5.3 and later,
23+
you can use an improved ``MongoClient.bulkWrite()`` method which can write
24+
to multiple databases and collections at once. See the :ref:`improved-bulk-write`
25+
section below to learn how to use the new method.
2726

2827
To perform a create, replace, update, or delete operation,
2928
use its corresponding method. For example, to insert one document,
@@ -307,55 +306,98 @@ see the following API Documentation:
307306
Improved Bulk Write Command
308307
---------------------------
309308

310-
.. specify which library
309+
Starting with {+mdb-server+} version 8.0 and {+driver-short+} version 5.3, there
310+
is an improved bulk write method, ``MongoClient.bulkWrite()``, that can be used
311+
to write to different databases and collections in the same cluster.
311312

312-
Starting with {+mdb-server+} version 8.0 and {+driver-short+} version 5.3, the
313-
``bulkWrite()`` method can be used in a different way. There are four ways in
314-
which you can call this method:
313+
In the previous examples on this page, the ``bulkWrite()`` method takes a list
314+
of ``WriteModel`` documents. The specified subclass of ``WriteModel`` represents
315+
the corresponding write operation. For example, ``InsertOneModel`` represents
316+
inserting one document. Similarly, the new ``bulkWrite()`` method takes a
317+
list of ``ClientNamespacedWriteModel`` objects to represent the write operation.
318+
However, you do not need to specify the subclass that represents the corresponding
319+
write operation. Instead, the ``ClientNamespacedWriteModel`` object contains different
320+
methods to represent different write operations, such as ``insertOne()`` or
321+
``updateOne()``. These methods take a ``MongoNamespace object that defines which
322+
database and collection to write to and a ``Document`` object that defines the
323+
document information. Some methods, such as ``updateOne()`` and ``replaceOne()``,
324+
also take a ``filter`` object.
325+
326+
The following sections describe how to use the new ``bulkWrite()`` method.
327+
328+
Insert Example
329+
~~~~~~~~~~~~~~
330+
331+
The following example shows how to use the new ``bulkWrite()`` method to insert
332+
two documents. One document is inserted into the ``people`` collection in our
333+
database. The other document is inserted into a collection called ``things``.
334+
We use the ``MongoNamespace`` object to define the database and collection we
335+
want each write operation to be applied to.
315336

316-
.. TODO: describe how these can be used to write to different collections at
317-
.. the same time
337+
.. code-block:: java
318338

319-
.. TODO: Namespace vs not namespace write model
339+
MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
340+
MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
320341

321-
bulkWrite(models)
322-
~~~~~~~~~~~~~~~~~
342+
ClientBulkWriteResult result = client.bulkWrite(List<>(
343+
ClientNamespacedWriteModel.insertOne(peopleNamespace, new Document("name", "Julia Smith")),
344+
ClientNamespacedWriteModel.insertOne(thingsNamespace, new Document("object", "washing machine"))
345+
));
323346

324-
In the previous examples on this page, the ``bulkWrite()`` method takes a list
325-
of ``WriteModel`` documents. In these examples, the specified subclass of
326-
``WriteModel`` represents the corresponding write operation, like ``InsertOneModel``
327-
represents inserting one document.
347+
After this example is executed, the ``people`` collection holds a
348+
``{ "name": "Julia Smith" }`` document and the ``things`` collection holds
349+
a ``{ "object": "washing machine" }`` document.
350+
351+
.. TODO: link documentation
328352

329-
Now, ``bulkWrite()`` can take a list of ``ClientWriteModel`` objects. Instead
330-
of having to specify different subclasses, this object has different methods to
331-
represent different write operations, such as ``insertOne()`` or ``updateOne()``.
332-
These methods take a ``MongoNamespace`` object that defines which database and
333-
collection to write to and a ``Document`` object that defines the document.
353+
Replace Example
354+
~~~~~~~~~~~~~~~
334355

335-
The following example shows how to use ``ClientWriteModel`` to insert two documents
336-
into the ``people`` collection of our database:
356+
The following example shows how to use the ``bulkWrite()`` method to replace an
357+
existing document in the ``people`` collection and an existing document in the
358+
``things`` collection.
337359

338360
.. code-block:: java
339-
MongoNamespace namespace = new MongoNamespace("db", "people");
361+
362+
MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
363+
MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
340364

341365
ClientBulkWriteResult result = client.bulkWrite(List<>(
342-
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Julia Smith")),
343-
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Blablabla"))
366+
ClientNamespacedWriteModel.replaceOne(
367+
peopleNamespace,
368+
Filters.eq("_id", 1),
369+
new Document("name", "Maximus Farquaad")
370+
),
371+
ClientNamespacedWriteModel.replaceOne(
372+
thingsNamespace,
373+
Filters.eq("_id", 1),
374+
new Document("object", "potato")
375+
)
344376
));
345377

346-
.. TODO: move code to includes
378+
After this example is executed, the document in the ``people`` collection in
379+
which the ``_id`` field has the value of ``1`` has been replaced with a new
380+
``{ "name": "Maximus Farquaad" }`` document. Also, the document
381+
in the ``things`` collection in which the ``_id`` field has the value of ``1``
382+
has been replaced with a ``{ "object": "potato" }`` document.
347383

348-
.. TODO: link documentation
384+
Bulk Write Options
385+
~~~~~~~~~~~~~~~~~~
386+
387+
Similarly to the :ref:`orderOfExecution` section above, you can use the
388+
``ClientBulkWriteOptions`` object to execute the new ``bulkWrite()`` method
389+
with options.
349390

350-
bulkWrite(models, options)
351-
~~~~~~~~~~~~~~~~~~~~~~~~~~
391+
Order of Execution Example
392+
``````````````````````````
352393

353-
Similarly to the :ref:`orderOfExecution` above, you can use the ``ClientBulkWriteOptions``
354-
object to add options to your call of the new ``bulkWrite()`` method.
394+
As described in the previous Order of Execution section, by default the write
395+
operations execute in the order they're specified. However, we can pass
396+
``false`` to the ``ordered()`` method on the ``ClientBulkWriteOptions`` object
397+
to execute write operations in any order.
355398

356-
As described above, by default the write operations execute in the order they're
357-
specified. However, we can specify ``false`` to the ``ordered()`` method on the
358-
``ClientBulkWriteOptions`` object to execute write operations in any order.
399+
The following code shows how to use the ``ordered()`` method on the
400+
``ClientBulkWriteOptions`` object.
359401

360402
.. code-block:: java
361403
MongoNamespace namespace = new MongoNamespace("db", "people");
@@ -364,21 +406,19 @@ specified. However, we can specify ``false`` to the ``ordered()`` method on the
364406

365407

366408
ClientBulkWriteResult result = client.bulkWrite(List<>(
367-
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Julia Smith")),
368-
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Blablabla"))
409+
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Donkey Kong")),
410+
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Mario"))
369411
),
370412
options);
371413

372-
.. TODO: add one more example?
373-
374414
.. TODO: link documentation
375415

376-
.. TODO; also results and error types
377-
378-
379416
Summary
380417
-------
381418

419+
``MongoCollection.bulkWrite()``
420+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
421+
382422
To perform a bulk operation, you create and pass a list of
383423
``WriteModel`` documents to the ``bulkWrite()`` method.
384424

@@ -392,4 +432,24 @@ There are two ways to execute the ``bulkWrite()`` method:
392432
- Unordered, which performs all the bulk operations in any order and reports errors
393433
at the end, if any
394434

395-
.. TODO: summarize results in a table i think
435+
``MongoClient.bulkWrite()``
436+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
437+
438+
In {+mdb-server+} version 8.0 and {+driver-short+} version 5.3 and later, you
439+
can use the ``bulkWrite()`` method to perform bulk operations on multiple
440+
databases and collections at once.
441+
442+
This method uses the ``ClientNamespacedWriteModel`` and its methods
443+
``insertOne()``, ``updateOne()``, ``updateMany()``, ``replaceOne()``,
444+
``deleteOne()``, and ``deleteMany()``.
445+
446+
The method can also take a ``ClientBulkWriteOptions`` object to specify different
447+
options for how the command is executed.
448+
449+
.. TODO: Add API Documentation
450+
451+
.. OPEN QUESTIONS FOR ENGINEERING:
452+
.. Should I include the extraneous types like the Client...Result object?
453+
.. Should I include the different ClientNamespacedInsertModel... etc?
454+
.. Is ClientWriteModel being exposed for public use?
455+
.. Is MongoCollection.bulkWrite() being deprecated?

source/whats-new.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ What's New
1919

2020
Learn what's new in:
2121

22+
* :ref:`Version 5.3 <java-version-5.3>`
2223
* :ref:`Version 5.2.1 <java-version-5.2.1>`
2324
* :ref:`Version 5.2 <java-version-5.2>`
2425
* :ref:`Version 5.1.3 <java-version-5.1.3>`
@@ -29,6 +30,16 @@ Learn what's new in:
2930
* :ref:`Version 4.11 <version-4.11>`
3031
* :ref:`Version 4.10 <version-4.10>`
3132

33+
.. _java-version-5.3:
34+
35+
What's New in 5.3
36+
-----------------
37+
38+
New features of the 5.3 release include:
39+
40+
- A new :ref:`bulk write command <improved-bulk-write>` that can perform bulk
41+
write operations on multiple databases and collections at once.
42+
3243
.. _java-version-5.2.1:
3344

3445
What's New in 5.2.1

0 commit comments

Comments
 (0)