Skip to content

Commit ac436b5

Browse files
authored
Merge pull request #438 from norareidy/DOCSP-31089-fix-bulk-examples
DOCSP-31089: Improve bulk operation examples
2 parents 3220f84 + 0af0d9f commit ac436b5

File tree

2 files changed

+97
-82
lines changed

2 files changed

+97
-82
lines changed

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MongoDB Java Driver.
2121
To perform a create, replace, update, or delete operation,
2222
use its corresponding method. For example, to insert one document,
2323
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.
2525

2626
The ``MongoClient`` performs these operations by making a call for each
2727
operation to the database. You can reduce the number of calls to the
@@ -31,18 +31,19 @@ Performing Bulk Operations
3131
--------------------------
3232

3333
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
3535
``bulkWrite()`` method. A ``WriteModel`` is a model that represents any
3636
of the write operations.
3737

3838
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:
4141

4242
.. code-block:: json
4343

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 }
4647

4748
For more information about the methods and classes mentioned in this section,
4849
see the following API Documentation:
@@ -62,7 +63,7 @@ Example
6263
```````
6364

6465
The following example creates an ``InsertOneModel`` for two documents
65-
where the ``_id`` values are "3" and "4":
66+
describing people:
6667

6768
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
6869
:language: java
@@ -77,7 +78,7 @@ where the ``_id`` values are "3" and "4":
7778
collection. Instead, the method throws a ``MongoBulkWriteException``.
7879

7980
The following example tries to insert two documents where the ``_id`` is
80-
"1" and "3":
81+
``1`` and ``3``:
8182

8283
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
8384
:language: java
@@ -90,12 +91,12 @@ where the ``_id`` values are "3" and "4":
9091
.. code-block:: shell
9192
:copyable: false
9293

93-
A MongoBulkWriteException occurred with the following message:
94+
A MongoBulkWriteException occurred with the following message:
9495
Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017.
9596
Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key
9697
error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
9798

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
99100
the :ref:`Order of Execution <orderOfExecution>` section.
100101

101102
For more information about the methods and classes mentioned in this section,
@@ -121,8 +122,8 @@ Example
121122
```````
122123

123124
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 added ``location`` field:
126127

127128
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
128129
:language: java
@@ -158,8 +159,7 @@ Example
158159
```````
159160

160161
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``:
163163

164164
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
165165
:language: java
@@ -195,7 +195,7 @@ Example
195195
```````
196196

197197
The following example creates a ``DeleteOneModel`` to delete
198-
a document where the ``_id`` is "1":
198+
a document where the ``_id`` is ``1``:
199199

200200
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
201201
:language: java
@@ -214,9 +214,9 @@ see the following API Documentation:
214214
Order of Execution
215215
------------------
216216

217-
The ``bulkWrite()`` method accepts an optional ``BulkWriteOptions`` as
218-
a second parameter to specify if you want to execute the bulk operations
219-
as ordered or unordered.
217+
The ``bulkWrite()`` method accepts an optional ``BulkWriteOptions`` as a
218+
second parameter to specify whether the execution of the bulk operations is
219+
ordered or unordered.
220220

221221
Ordered Execution
222222
~~~~~~~~~~~~~~~~~
@@ -230,10 +230,13 @@ Example
230230

231231
The following example performs these bulk operations:
232232

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 operation that inserts a document with a ``name`` value of ``"Zaynab Omar"`` and an
234+
``age`` value of ``37``
235+
- An operation that replaces the document where the ``_id`` is ``1`` with a new document
236+
that contains the ``location`` field
237+
- An operation that updates the document with a ``name`` value of ``"Zaynab Omar"`` and
238+
changes the ``name`` to ``"Zaynab Hassan"``
239+
- An operation that deletes all documents where the ``age`` value is greater than ``50``
237240

238241
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
239242
:language: java
@@ -247,7 +250,9 @@ document:
247250
.. code-block:: json
248251
:copyable: false
249252

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 }
251256

252257
Unordered Execution
253258
~~~~~~~~~~~~~~~~~~~
@@ -280,8 +285,9 @@ operations to execute in any order:
280285
.. code-block:: json
281286
:copyable: false
282287

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 }
285291

286292
For more information about the methods and classes mentioned in this section,
287293
see the following API Documentation:
@@ -302,4 +308,5 @@ There are 6 different ``WriteModel`` documents: ``InsertOneModel``,
302308
There are two ways to execute the ``bulkWrite()`` method:
303309

304310
- Ordered, which performs the bulk operations in order until an error occurs, if any
305-
- Unordered, which performs all the bulk operations in any order and reports errors at the end, if any
311+
- Unordered, which performs all the bulk operations in any order and reports errors
312+
at the end, if any

source/includes/fundamentals/code-snippets/BulkWrite.java

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,22 @@ public static void main(String[] args) {
5252
bulkWrite.insertExceptionExample();
5353

5454
System.out.println("Insert");
55+
bulkWrite.setUpCollection();
5556
bulkWrite.insertDocumentsExample();
5657
bulkWrite.preview();
5758

5859
System.out.println("Replace");
60+
bulkWrite.setUpCollection();
5961
bulkWrite.replaceDocumentsExample();
6062
bulkWrite.preview();
6163

6264
System.out.println("Update");
65+
bulkWrite.setUpCollection();
6366
bulkWrite.updateDocumentsExample();
6467
bulkWrite.preview();
6568

6669
System.out.println("Delete");
70+
bulkWrite.setUpCollection();
6771
bulkWrite.deleteDocumentsExample();
6872
bulkWrite.preview();
6973
}
@@ -74,11 +78,11 @@ private void insertExceptionExample() {
7478
try {
7579
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
7680

77-
InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 1));
78-
InsertOneModel<Document> doc4 = new InsertOneModel<>(new Document("_id", 3));
81+
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
82+
InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 3));
7983

84+
bulkOperations.add(doc1);
8085
bulkOperations.add(doc3);
81-
bulkOperations.add(doc4);
8286

8387
collection.bulkWrite(bulkOperations);
8488

@@ -91,16 +95,22 @@ private void insertExceptionExample() {
9195
private void bulkWriteNotOrderedExample() {
9296
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
9397

94-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 3));
95-
ReplaceOneModel<Document> doc2 = new ReplaceOneModel<>(Filters.eq("_id", 1),
96-
new Document("_id", 1).append("x", 2));
97-
UpdateOneModel<Document> doc3 = new UpdateOneModel<>(Filters.eq("_id", 3), Updates.set("x", 2));
98-
DeleteManyModel<Document> doc4 = new DeleteManyModel<>(Filters.eq("x", 2));
98+
99+
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
100+
.append("name", "Zaynab Omar")
101+
.append("age", 37));
102+
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
103+
new Document("name", "Sandy Kane")
104+
.append("location", "Helena, MT"));
105+
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
106+
Updates.set("name", "Zaynab Hassan"));
107+
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
99108

100-
bulkOperations.add(doc1);
101-
bulkOperations.add(doc2);
102-
bulkOperations.add(doc3);
103-
bulkOperations.add(doc4);
109+
bulkOperations.add(insertDoc);
110+
bulkOperations.add(replaceDoc);
111+
bulkOperations.add(updateDoc);
112+
bulkOperations.add(deleteDoc);
113+
104114

105115
// begin bulkWriteNotOrderedExample
106116
BulkWriteOptions options = new BulkWriteOptions().ordered(false);
@@ -114,90 +124,79 @@ private void bulkWriteExample() {
114124

115125
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
116126

117-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 3));
118-
ReplaceOneModel<Document> doc2 = new ReplaceOneModel<>(Filters.eq("_id", 1),
119-
new Document("_id", 1).append("x", 2));
120-
UpdateOneModel<Document> doc3 = new UpdateOneModel<>(Filters.eq("_id", 3), Updates.set("x", 2));
121-
DeleteManyModel<Document> doc4 = new DeleteManyModel<>(Filters.eq("x", 2));
127+
128+
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
129+
.append("name", "Zaynab Omar")
130+
.append("age", 37));
131+
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
132+
new Document("name", "Sandy Kane")
133+
.append("location", "Helena, MT"));
134+
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
135+
Updates.set("name", "Zaynab Hassan"));
136+
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
122137

123-
bulkOperations.add(doc1);
124-
bulkOperations.add(doc2);
125-
bulkOperations.add(doc3);
126-
bulkOperations.add(doc4);
138+
bulkOperations.add(insertDoc);
139+
bulkOperations.add(replaceDoc);
140+
bulkOperations.add(updateDoc);
141+
bulkOperations.add(deleteDoc);
127142

128143
collection.bulkWrite(bulkOperations);
129144
//end bulkWriteExample
130145
}
131146

132147
private void insertDocumentsExample(){
133-
collection.drop();
134148
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
135149

136150
// begin insertDocumentsExample
137-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 3));
138-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 4));
151+
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie")
152+
.append("age", 17));
153+
InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss")
154+
.append("age", 22));
139155
//end insertDocumentsExample
140156

141-
bulkOperations.add(doc1);
142-
bulkOperations.add(doc2);
157+
bulkOperations.add(juneDoc);
158+
bulkOperations.add(kevinDoc);
143159

144160
collection.bulkWrite(bulkOperations);
145161
}
146162

147163
private void replaceDocumentsExample(){
148-
collection.drop();
149164
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
150165

151-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
152-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
153-
154166
// begin replaceDocumentsExample
155-
ReplaceOneModel<Document> doc3 = new ReplaceOneModel<>(
167+
ReplaceOneModel<Document> celineDoc = new ReplaceOneModel<>(
156168
Filters.eq("_id", 1),
157-
new Document("_id", 1).append("x", 4));
169+
new Document("name", "Celine Stork")
170+
.append("location", "San Diego, CA"));
158171
//end replaceDocumentsExample
159172

160-
bulkOperations.add(doc1);
161-
bulkOperations.add(doc2);
162-
bulkOperations.add(doc3);
173+
bulkOperations.add(celineDoc);
163174

164175
collection.bulkWrite(bulkOperations);
165176
}
166177

167178
private void updateDocumentsExample(){
168-
collection.drop();
169179
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
170180

171-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
172-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
173-
174181
// begin updateDocumentsExample
175-
UpdateOneModel<Document> doc3 = new UpdateOneModel<>(
182+
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(
176183
Filters.eq("_id", 2),
177-
Updates.set("x", 8));
184+
Updates.set("age", 31));
178185
//end updateDocumentsExample
179186

180-
bulkOperations.add(doc1);
181-
bulkOperations.add(doc2);
182-
bulkOperations.add(doc3);
187+
bulkOperations.add(updateDoc);
183188

184189
collection.bulkWrite(bulkOperations);
185190
}
186191

187192
private void deleteDocumentsExample(){
188-
collection.drop();
189193
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
190194

191-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
192-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
193-
194195
// begin deleteDocumentsExample
195-
DeleteOneModel<Document> doc3 = new DeleteOneModel<>(Filters.eq("_id", 1));
196+
DeleteOneModel<Document> deleteDoc = new DeleteOneModel<>(Filters.eq("_id", 1));
196197
//end deleteDocumentsExample
197198

198-
bulkOperations.add(doc1);
199-
bulkOperations.add(doc2);
200-
bulkOperations.add(doc3);
199+
bulkOperations.add(deleteDoc);
201200

202201
collection.bulkWrite(bulkOperations);
203202
}
@@ -213,11 +212,20 @@ private void setUpCollection(){
213212
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
214213
//end bulkOpsList
215214

216-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
217-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
215+
InsertOneModel<Document> karen = new InsertOneModel<>(new Document("_id", 1)
216+
.append("name", "Karen Sandoval")
217+
.append("age", 31));
218+
InsertOneModel<Document> william = new InsertOneModel<>(new Document("_id", 2)
219+
.append("name", "William Chin")
220+
.append("age", 54));
221+
InsertOneModel<Document> shayla = new InsertOneModel<>(new Document("_id", 8)
222+
.append("name", "Shayla Ray")
223+
.append("age", 20));
218224

219-
bulkOperations.add(doc1);
220-
bulkOperations.add(doc2);
225+
bulkOperations.add(karen);
226+
bulkOperations.add(william);
227+
bulkOperations.add(shayla);
228+
221229

222230
collection.bulkWrite(bulkOperations);
223231
}

0 commit comments

Comments
 (0)