Skip to content

Commit 72b9e73

Browse files
committed
initial checkpoint
1 parent ce8ee19 commit 72b9e73

File tree

1 file changed

+86
-1
lines changed
  • source/fundamentals/crud/write-operations

1 file changed

+86
-1
lines changed

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Overview
1616
In this guide, you can learn how to use bulk operations in the
1717
MongoDB Java Driver.
1818

19+
.. note:: Improved Bulk Write Commands
20+
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>
27+
1928
To perform a create, replace, update, or delete operation,
2029
use its corresponding method. For example, to insert one document,
2130
update multiple documents, and delete one document in your collection,
@@ -44,7 +53,7 @@ document. The examples in each section use the following documents in the
4453
{ "_id": 8, "name": "Shayla Ray", "age": 20 }
4554

4655
For more information about the methods and classes mentioned in this section,
47-
see the following API Documentation:
56+
see the following API documentation:
4857

4958
- `bulkWrite() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#bulkWrite(java.util.List,com.mongodb.client.model.BulkWriteOptions)>`__
5059
- `WriteModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/WriteModel.html>`__
@@ -293,6 +302,80 @@ see the following API Documentation:
293302
- `BulkWriteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html>`__
294303
- `ordered() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html#ordered(boolean)>`__
295304

305+
.. _improved-bulk-write:
306+
307+
Improved Bulk Write Command
308+
---------------------------
309+
310+
.. specify which library
311+
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:
315+
316+
.. TODO: describe how these can be used to write to different collections at
317+
.. the same time
318+
319+
.. TODO: Namespace vs not namespace write model
320+
321+
bulkWrite(models)
322+
~~~~~~~~~~~~~~~~~
323+
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.
328+
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.
334+
335+
The following example shows how to use ``ClientWriteModel`` to insert two documents
336+
into the ``people`` collection of our database:
337+
338+
.. code-block:: java
339+
MongoNamespace namespace = new MongoNamespace("db", "people");
340+
341+
ClientBulkWriteResult result = client.bulkWrite(List<>(
342+
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Julia Smith")),
343+
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Blablabla"))
344+
));
345+
346+
.. TODO: move code to includes
347+
348+
.. TODO: link documentation
349+
350+
bulkWrite(models, options)
351+
~~~~~~~~~~~~~~~~~~~~~~~~~~
352+
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.
355+
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.
359+
360+
.. code-block:: java
361+
MongoNamespace namespace = new MongoNamespace("db", "people");
362+
363+
ClientBulkWriteOptions options = new ClientBulkWriteOptions().ordered(false);
364+
365+
366+
ClientBulkWriteResult result = client.bulkWrite(List<>(
367+
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Julia Smith")),
368+
ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Blablabla"))
369+
),
370+
options);
371+
372+
.. TODO: add one more example?
373+
374+
.. TODO: link documentation
375+
376+
.. TODO; also results and error types
377+
378+
296379
Summary
297380
-------
298381

@@ -308,3 +391,5 @@ There are two ways to execute the ``bulkWrite()`` method:
308391
- Ordered, which performs the bulk operations in order until an error occurs, if any
309392
- Unordered, which performs all the bulk operations in any order and reports errors
310393
at the end, if any
394+
395+
.. TODO: summarize results in a table i think

0 commit comments

Comments
 (0)