|
| 1 | +.. _javars-bulk-writes: |
| 2 | + |
| 3 | +===================== |
| 4 | +Bulk Write Operations |
| 5 | +===================== |
| 6 | + |
| 7 | +Starting in v2.6, MongoDB supports bulk write commands |
| 8 | +for insert, update, and delete operations in a way that allows the |
| 9 | +driver to implement the correct semantics for ``BulkWriteResult`` and |
| 10 | +``BulkWriteException``. |
| 11 | + |
| 12 | +There are two types of bulk operations, ordered and unordered bulk |
| 13 | +operations: |
| 14 | + |
| 15 | +1. Ordered bulk operations execute all the operations in order and |
| 16 | + error out on the first write error. |
| 17 | +#. Unordered bulk operations execute all the operations and report any |
| 18 | + the errors. Unordered bulk operations do not guarantee an order of |
| 19 | + execution. |
| 20 | + |
| 21 | +.. important:: |
| 22 | + |
| 23 | + This guide uses the ``Subscriber`` implementations, which are |
| 24 | + described in the :ref:`Quick Start Primer <javars-primer>`. |
| 25 | + |
| 26 | +The following code provides examples using ordered and unordered |
| 27 | +operations: |
| 28 | + |
| 29 | +.. code-block:: java |
| 30 | + |
| 31 | + // Ordered bulk operation - order is guaranteed |
| 32 | + collection.bulkWrite( |
| 33 | + Arrays.asList(new InsertOneModel<>(new Document("_id", 4)), |
| 34 | + new InsertOneModel<>(new Document("_id", 5)), |
| 35 | + new InsertOneModel<>(new Document("_id", 6)), |
| 36 | + new UpdateOneModel<>(new Document("_id", 1), |
| 37 | + new Document("$set", new Document("x", 2))), |
| 38 | + new DeleteOneModel<>(new Document("_id", 2)), |
| 39 | + new ReplaceOneModel<>(new Document("_id", 3), |
| 40 | + new Document("_id", 3).append("x", 4)))) |
| 41 | + .subscribe(new ObservableSubscriber<BulkWriteResult>()); |
| 42 | + |
| 43 | + // Unordered bulk operation - no guarantee of order of operation |
| 44 | + collection.bulkWrite( |
| 45 | + Arrays.asList(new InsertOneModel<>(new Document("_id", 4)), |
| 46 | + new InsertOneModel<>(new Document("_id", 5)), |
| 47 | + new InsertOneModel<>(new Document("_id", 6)), |
| 48 | + new UpdateOneModel<>(new Document("_id", 1), |
| 49 | + new Document("$set", new Document("x", 2))), |
| 50 | + new DeleteOneModel<>(new Document("_id", 2)), |
| 51 | + new ReplaceOneModel<>(new Document("_id", 3), |
| 52 | + new Document("_id", 3).append("x", 4))), |
| 53 | + new BulkWriteOptions().ordered(false)) |
| 54 | + .subscribe(new ObservableSubscriber<BulkWriteResult>()); |
0 commit comments