|
| 1 | +======================= |
| 2 | +Perform Bulk Operations |
| 3 | +======================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +:node-api:`collection.bulkWrite <Collection.html#bulkWrite>` lets you |
| 11 | +perform bulk write operations against a *single* collection. With |
| 12 | +``collection.bulkWrite()``, you specify a list of operations to perform and |
| 13 | +MongoDB then executes those operations in bulk. Bulk write supports |
| 14 | +``insertOne``, ``updateOne``, ``updateMany``, ``deleteOne``, |
| 15 | +``deleteMany``, and ``replaceOne`` operations. Refer to the method |
| 16 | +documentation for full details. |
| 17 | + |
| 18 | +``bulkWrite()`` accepts the following parameters: |
| 19 | + |
| 20 | +- ``operations``: specifies the bulk operations to |
| 21 | + perform. Each operation is passed to ``bulkWrite()`` as an object in |
| 22 | + an array. |
| 23 | + |
| 24 | +- ``options``: *optional* settings that affect the execution |
| 25 | + of the operation, such as :manual:`write concern |
| 26 | + </reference/write-concern>` and order. |
| 27 | + |
| 28 | + By default, MongoDB executes bulk operations one-by-one in the |
| 29 | + specified order (i.e. serially). During an ordered bulk write, if |
| 30 | + an error occurs during the processing of an operation, MongoDB returns without |
| 31 | + processing the remaining operations in the list. In contrast, |
| 32 | + when ``ordered`` is ``false``, MongoDB continues to process remaining |
| 33 | + write operations in the list. Unordered operations are faster as |
| 34 | + MongoDB can execute the operations in parallel, but the results of the |
| 35 | + operation may vary. For example, a ``deleteOne`` operation run before |
| 36 | + an ``updateMany`` operation might have a different result from running |
| 37 | + it after the ``updateMany``. Refer to :manual:`Execution of Operations |
| 38 | + </reference/method/db.collection.bulkWrite/#execution-of-operations>` for more |
| 39 | + information. |
| 40 | + |
| 41 | +- ``callback``: command result callback. Like other collection methods, |
| 42 | + ``bulkWrite()`` returns a Promise if you do not specify a callback. The |
| 43 | + Promise resolves to a :node-api:`bulkWriteOpCallback </Collection.html#~bulkWriteOpCallback>` object containing the |
| 44 | + result of the operation. |
| 45 | + |
| 46 | +``bulkWrite()`` returns a ``BulkWriteResult`` object |
| 47 | +that includes the number of updated or inserted documents, the ``_id`` of |
| 48 | +each document, and any ``writeError`` or ``writeConcernErrors``. Refer to |
| 49 | +the documentation for :node-api:`BulkWriteResult </BulkWriteResult>` for |
| 50 | +more information. |
| 51 | + |
| 52 | +If you create an index with a :manual:`unique constraint |
| 53 | +</core/index-unique>`, you might encounter a duplicate key write error |
| 54 | +during an operation. The following example shows a duplicate key error |
| 55 | +encountered when two of the users in the inserted sample dataset had the |
| 56 | +same email address. |
| 57 | + |
| 58 | +.. code-block:: sh |
| 59 | + |
| 60 | + Error during bulkWrite, BulkWriteError: E11000 duplicate key error collection: sample_mflix.users index: email_1 dup key: { : " [email protected]" } |
| 61 | + |
| 62 | +Similarly, if you attempt to perform a bulk write against a collection |
| 63 | +that uses :manual:`schema validation </core/schema-validation>`, you may |
| 64 | +encounter warnings or errors related to the formatting of inserted or modified |
| 65 | +documents. |
| 66 | + |
| 67 | +Example |
| 68 | +------- |
| 69 | + |
| 70 | +The following code sample performs a bulk write operation against the |
| 71 | +``users`` collection in the ``sample_mflix`` database. Specifically, the |
| 72 | +program formats the data from a ``users.json`` source file to perform a |
| 73 | +series of ``insertOne`` operations with the ``bulkWrite()`` collection |
| 74 | +method. Download the dataset here: `users.json |
| 75 | +<https://raw.githubusercontent.com/mongodb-university/universal-driver-examples/master/users.json>`_. |
| 76 | + |
| 77 | +Documents in the ``users`` collection have ``email``, ``password``, |
| 78 | +and ``name`` fields, as well as the generated ``_id`` field. |
| 79 | +``users.json`` contains an array of objects that map directly to the |
| 80 | +existing fields in the collection, as in the following: |
| 81 | + |
| 82 | +.. code-block:: javascript |
| 83 | + |
| 84 | + { |
| 85 | + |
| 86 | + "password" : "450f6704710dcf8033157978f7fbdfde", |
| 87 | + "name" : "Marie Conrad" |
| 88 | + } |
| 89 | + |
| 90 | +.. literalinclude:: /code-snippets/usage-examples/bulkWrite-example.js |
| 91 | + :language: javascript |
| 92 | + |
| 93 | + |
0 commit comments