@@ -73,68 +73,84 @@ private void insertExceptionExample() {
73
73
// begin insertExceptionExample
74
74
try {
75
75
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
76
-
76
+
77
+ // Creates instructions to insert documents
77
78
InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
78
79
InsertOneModel <Document > doc3 = new InsertOneModel <>(new Document ("_id" , 3 ));
79
80
80
81
bulkOperations .add (doc1 );
81
82
bulkOperations .add (doc3 );
82
83
84
+ // Runs a bulk write operation for the specified insert WriteModels
83
85
collection .bulkWrite (bulkOperations );
84
86
87
+ // Prints a message if any exceptions occur during the bulk write operation
85
88
} catch (MongoBulkWriteException e ){
86
- System .out .println ("A MongoBulkWriteException occured with the following message: " + e .getMessage ());
89
+ System .out .println ("A MongoBulkWriteException occurred with the following message: " + e .getMessage ());
87
90
}
88
91
//end insertExceptionExample
89
92
}
90
93
91
94
private void bulkWriteNotOrderedExample () {
92
95
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
93
96
94
-
97
+ // Creates instructions to insert a document
95
98
InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
96
99
.append ("name" , "Zaynab Omar" )
97
100
.append ("age" , 37 ));
101
+
102
+ // Creates instructions to replace the first document that matches the query
98
103
ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
99
104
new Document ("name" , "Sandy Kane" )
100
- .append ("location" , "Helena, MT" ));
105
+ .append ("location" , "Helena, MT" ));
106
+
107
+ // Creates instructions to update the first document that matches the query
101
108
UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
102
109
Updates .set ("name" , "Zaynab Hassan" ));
110
+
111
+ // Creates instructions to delete all documents that match the query
103
112
DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
104
113
105
- bulkOperations .add (doc1 );
106
- bulkOperations .add (doc2 );
107
- bulkOperations .add (doc3 );
108
- bulkOperations .add (doc4 );
109
-
114
+ bulkOperations .add (insertDoc );
115
+ bulkOperations .add (replaceDoc );
116
+ bulkOperations .add (updateDoc );
117
+ bulkOperations .add (deleteDoc );
118
+
110
119
// begin bulkWriteNotOrderedExample
111
120
BulkWriteOptions options = new BulkWriteOptions ().ordered (false );
112
121
122
+ // Runs a bulk write operation for the specified insert, replace, update, and delete WriteModels in any order
113
123
collection .bulkWrite (bulkOperations , options );
114
124
//end bulkWriteNotOrderedExample
115
125
}
116
126
117
127
private void bulkWriteExample () {
118
128
// begin bulkWriteExample
119
-
120
129
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
121
130
122
-
131
+ // Creates instructions to insert a document
123
132
InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
124
- .append ("name" , "Zaynab Omar" )
125
- .append ("age" , 37 ));
133
+ .append ("name" , "Zaynab Omar" )
134
+ .append ("age" , 37 ));
135
+
136
+ // Creates instructions to replace the first document matched by the query
126
137
ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
127
- new Document ("name" , "Sandy Kane" )
128
- .append ("location" , "Helena, MT" ));
129
- UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
138
+ new Document ("name" , "Sandy Kane" )
139
+ .append ("location" , "Helena, MT" ));
140
+
141
+ // Creates instructions to update the first document matched by the query
142
+ UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
130
143
Updates .set ("name" , "Zaynab Hassan" ));
144
+
145
+ // Creates instructions to delete all documents matched by the query
131
146
DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
132
147
133
148
bulkOperations .add (doc1 );
134
149
bulkOperations .add (doc2 );
135
150
bulkOperations .add (doc3 );
136
151
bulkOperations .add (doc4 );
137
152
153
+ // Runs a bulk write operation for the specified the insert, replace, update, and delete WriteModels in order
138
154
collection .bulkWrite (bulkOperations );
139
155
//end bulkWriteExample
140
156
}
@@ -143,14 +159,19 @@ private void insertDocumentsExample(){
143
159
collection .drop ();
144
160
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
145
161
162
+ // Creates instructions to insert multiple documents
146
163
// begin insertDocumentsExample
147
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 3 ));
148
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 4 ));
164
+ InsertOneModel <Document > juneDoc = new InsertOneModel <>(new Document ("name" , "June Carrie" )
165
+ .append ("age" , 17 ));
166
+
167
+ InsertOneModel <Document > kevinDoc = new InsertOneModel <>(new Document ("name" , "Kevin Moss" )
168
+ .append ("age" , 22 ));
149
169
//end insertDocumentsExample
150
170
151
171
bulkOperations .add (doc1 );
152
172
bulkOperations .add (doc2 );
153
173
174
+ // Runs a bulk write operation for the specified insert WriteModels
154
175
collection .bulkWrite (bulkOperations );
155
176
}
156
177
@@ -161,6 +182,7 @@ private void replaceDocumentsExample(){
161
182
InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
162
183
InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
163
184
185
+ // Creates instructions to replace the first document matched by the query
164
186
// begin replaceDocumentsExample
165
187
ReplaceOneModel <Document > doc3 = new ReplaceOneModel <>(
166
188
Filters .eq ("_id" , 1 ),
@@ -172,6 +194,7 @@ private void replaceDocumentsExample(){
172
194
bulkOperations .add (doc2 );
173
195
bulkOperations .add (doc3 );
174
196
197
+ // Runs a bulk write operation for the specified replace WriteModel
175
198
collection .bulkWrite (bulkOperations );
176
199
}
177
200
@@ -182,6 +205,7 @@ private void updateDocumentsExample(){
182
205
InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
183
206
InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
184
207
208
+ // Creates instructions to update the first document matched by the query
185
209
// begin updateDocumentsExample
186
210
UpdateOneModel <Document > doc3 = new UpdateOneModel <>(
187
211
Filters .eq ("_id" , 2 ),
@@ -192,6 +216,7 @@ private void updateDocumentsExample(){
192
216
bulkOperations .add (doc2 );
193
217
bulkOperations .add (doc3 );
194
218
219
+ // Runs a bulk write operation for the specified update WriteModel
195
220
collection .bulkWrite (bulkOperations );
196
221
}
197
222
@@ -202,6 +227,7 @@ private void deleteDocumentsExample(){
202
227
InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
203
228
InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
204
229
230
+ // Creates instructions to delete the first document matched by the query
205
231
// begin deleteDocumentsExample
206
232
DeleteOneModel <Document > doc3 = new DeleteOneModel <>(Filters .eq ("_id" , 1 ));
207
233
//end deleteDocumentsExample
@@ -210,6 +236,7 @@ private void deleteDocumentsExample(){
210
236
bulkOperations .add (doc2 );
211
237
bulkOperations .add (doc3 );
212
238
239
+ // Runs a bulk write operation for the specified delete WriteModel
213
240
collection .bulkWrite (bulkOperations );
214
241
}
215
242
@@ -227,15 +254,18 @@ private void setUpCollection(){
227
254
InsertOneModel <Document > karen = new InsertOneModel <>(new Document ("_id" , 1 )
228
255
.append ("name" , "Karen Sandoval" )
229
256
.append ("age" , 31 ));
257
+
230
258
InsertOneModel <Document > william = new InsertOneModel <>(new Document ("_id" , 2 )
231
259
.append ("name" , "William Chin" )
232
260
.append ("age" , 54 ));
261
+
233
262
InsertOneModel <Document > shayla = new InsertOneModel <>(new Document ("_id" , 8 )
234
263
.append ("name" , "Shayla Ray" )
235
264
.append ("age" , 20 ));
236
-
237
- bulkOperations .add (doc1 );
238
- bulkOperations .add (doc2 );
265
+
266
+ bulkOperations .add (karen );
267
+ bulkOperations .add (william );
268
+ bulkOperations .add (shayla );
239
269
240
270
collection .bulkWrite (bulkOperations );
241
271
}
0 commit comments