@@ -21,7 +21,7 @@ MongoDB Java Driver.
21
21
To perform a create, replace, update, or delete operation,
22
22
use its corresponding method. For example, to insert one document,
23
23
update multiple documents, and delete one document in your collection,
24
- use the ``insertOne()``, ``updateMany()`` and ``deleteOne()`` methods.
24
+ use the ``insertOne()``, ``updateMany()`` and ``deleteOne()`` methods.
25
25
26
26
The ``MongoClient`` performs these operations by making a call for each
27
27
operation to the database. You can reduce the number of calls to the
@@ -31,18 +31,19 @@ Performing Bulk Operations
31
31
--------------------------
32
32
33
33
Bulk operations consist of a large number of write operations. To perform
34
- a bulk operation, pass a ``List`` of ``WriteModel`` documents to the
34
+ a bulk operation, pass a ``List`` of ``WriteModel`` documents to the
35
35
``bulkWrite()`` method. A ``WriteModel`` is a model that represents any
36
36
of the write operations.
37
37
38
38
The following sections show how to create and use each ``WriteModel``
39
- document. The examples in each section contain the following documents
40
- in a collection:
39
+ document. The examples in each section use the following documents in the
40
+ ``people`` collection:
41
41
42
42
.. code-block:: json
43
43
44
- { "_id": 1 }
45
- { "_id": 2 }
44
+ { "_id": 1, "name": "Karen Sandoval", "age": 31 }
45
+ { "_id": 2, "name": "William Chin", "age": 54 }
46
+ { "_id": 8, "name": "Shayla Ray", "age": 20 }
46
47
47
48
For more information about the methods and classes mentioned in this section,
48
49
see the following API Documentation:
@@ -62,7 +63,7 @@ Example
62
63
```````
63
64
64
65
The following example creates an ``InsertOneModel`` for two documents
65
- where the ``_id`` values are "3" and "4":
66
+ describing people:
66
67
67
68
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
68
69
:language: java
@@ -77,7 +78,7 @@ where the ``_id`` values are "3" and "4":
77
78
collection. Instead, the method throws a ``MongoBulkWriteException``.
78
79
79
80
The following example tries to insert two documents where the ``_id`` is
80
- "1" and "3" :
81
+ ``1`` and ``3`` :
81
82
82
83
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
83
84
:language: java
@@ -90,12 +91,12 @@ where the ``_id`` values are "3" and "4":
90
91
.. code-block:: shell
91
92
:copyable: false
92
93
93
- A MongoBulkWriteException occurred with the following message:
94
+ A MongoBulkWriteException occurred with the following message:
94
95
Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017.
95
96
Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key
96
97
error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
97
98
98
- To see why the document with the ``_id`` of "3" didn't insert, see
99
+ To see why the document with the ``_id`` of ``3`` didn't insert, see
99
100
the :ref:`Order of Execution <orderOfExecution>` section.
100
101
101
102
For more information about the methods and classes mentioned in this section,
@@ -121,8 +122,8 @@ Example
121
122
```````
122
123
123
124
The following example creates a ``ReplaceOneModel`` to
124
- replace a document where the ``_id`` is "1" with a document that
125
- contains an additional field:
125
+ replace a document where the ``_id`` is ``1`` with a document that
126
+ contains an additional ``location`` field:
126
127
127
128
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
128
129
:language: java
@@ -158,8 +159,7 @@ Example
158
159
```````
159
160
160
161
The following example creates an ``UpdateOneModel`` to update
161
- a document where the ``_id`` is "2" to a document that
162
- contains an additional field:
162
+ the ``age`` field in a document where the ``_id`` is ``2``:
163
163
164
164
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
165
165
:language: java
@@ -195,7 +195,7 @@ Example
195
195
```````
196
196
197
197
The following example creates a ``DeleteOneModel`` to delete
198
- a document where the ``_id`` is "1" :
198
+ a document where the ``_id`` is ``1`` :
199
199
200
200
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
201
201
:language: java
@@ -230,10 +230,13 @@ Example
230
230
231
231
The following example performs these bulk operations:
232
232
233
- - An insert operation for a document where the ``_id`` is "3"
234
- - A replace operation for a document where the ``_id`` is "1" with a document that contains an additional field
235
- - An update operation for a document where the ``_id`` is "3" to a document that contains an additional field
236
- - A delete operation for all documents that contain the field ``x`` with the value "2"
233
+ - An insert operation for a document where the ``name`` is ``"Zaynab Omar"``and the
234
+ ``age`` is ``37``
235
+ - A replace operation for a document where the ``_id`` is ``1`` with a new document
236
+ that contains the ``location`` field
237
+ - An update operation for a document where the ``name`` is ``"Zaynab Omar"`` to change the ``name``
238
+ field to ``"Zaynab Hassan"``
239
+ - A delete operation for all documents that have an ``age`` value greater than ``50``
237
240
238
241
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
239
242
:language: java
@@ -247,7 +250,9 @@ document:
247
250
.. code-block:: json
248
251
:copyable: false
249
252
250
- { "_id": 2 }
253
+ { "_id": 1, "name": "Sandy Kane", "location": "Helena, MT" }
254
+ { "_id": 8, "name": "Shayla Ray", "age": 20 }
255
+ { "_id": 6, "name": "Zaynab Hassan", "age": 37 }
251
256
252
257
Unordered Execution
253
258
~~~~~~~~~~~~~~~~~~~
@@ -280,8 +285,9 @@ operations to execute in any order:
280
285
.. code-block:: json
281
286
:copyable: false
282
287
283
- { "_id": 2 }
284
- { "_id": 3 }
288
+ { "_id": 1, "name": "Sandy Kane", "location": "Helena, MT" }
289
+ { "_id": 8, "name": "Shayla Ray", "age": 20 }
290
+ { "_id": 6, "name": "Zaynab Omar", "age": 37 }
285
291
286
292
For more information about the methods and classes mentioned in this section,
287
293
see the following API Documentation:
0 commit comments