Skip to content

Commit e67af2d

Browse files
committed
DOCSP-31089: Improve bulk operation examples
1 parent 3220f84 commit e67af2d

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

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

Lines changed: 28 additions & 22 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 additional ``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
@@ -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 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``
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:

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ private void bulkWriteExample() {
114114

115115
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
116116

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));
117+
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("name", "Zaynab Omar").append("age", 37));
118+
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
119+
new Document("name", "Sandy Kane").append("location", "Helena, MT"));
120+
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"), Updates.set("name", "Zaynab Hassan"));
121+
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
122122

123-
bulkOperations.add(doc1);
124-
bulkOperations.add(doc2);
125-
bulkOperations.add(doc3);
126-
bulkOperations.add(doc4);
123+
bulkOperations.add(insertDoc);
124+
bulkOperations.add(replaceDoc);
125+
bulkOperations.add(updateDoc);
126+
bulkOperations.add(deleteDoc);
127127

128128
collection.bulkWrite(bulkOperations);
129129
//end bulkWriteExample
@@ -134,8 +134,8 @@ private void insertDocumentsExample(){
134134
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
135135

136136
// begin insertDocumentsExample
137-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 3));
138-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 4));
137+
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie").append("age", 17));
138+
InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss").append("age", 22));
139139
//end insertDocumentsExample
140140

141141
bulkOperations.add(doc1);
@@ -152,9 +152,9 @@ private void replaceDocumentsExample(){
152152
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
153153

154154
// begin replaceDocumentsExample
155-
ReplaceOneModel<Document> doc3 = new ReplaceOneModel<>(
155+
ReplaceOneModel<Document> celineDoc = new ReplaceOneModel<>(
156156
Filters.eq("_id", 1),
157-
new Document("_id", 1).append("x", 4));
157+
new Document("name", "Celine Stork").append("location", "San Diego, CA"));
158158
//end replaceDocumentsExample
159159

160160
bulkOperations.add(doc1);
@@ -172,9 +172,9 @@ private void updateDocumentsExample(){
172172
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
173173

174174
// begin updateDocumentsExample
175-
UpdateOneModel<Document> doc3 = new UpdateOneModel<>(
175+
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(
176176
Filters.eq("_id", 2),
177-
Updates.set("x", 8));
177+
Updates.set("age", 31));
178178
//end updateDocumentsExample
179179

180180
bulkOperations.add(doc1);

0 commit comments

Comments
 (0)