@@ -30,12 +30,14 @@ public class BulkWrite {
30
30
private BulkWrite () {
31
31
final String uri = System .getenv ("DRIVER_REF_URI" );
32
32
33
+ // Create a client and access the "bulkWrite" collection in the "crudOps" database
33
34
mongoClient = MongoClients .create (uri );
34
35
database = mongoClient .getDatabase ("crudOps" );
35
36
collection = database .getCollection ("bulkWrite" );
36
37
}
37
38
38
39
public static void main (String [] args ) {
40
+ // For each operation, set up the collection, run the operation, and print the results
39
41
BulkWrite bulkWrite = new BulkWrite ();
40
42
System .out .println ("Ordered BulkWrite" );
41
43
bulkWrite .setUpCollection ();
@@ -76,157 +78,207 @@ public static void main(String[] args) {
76
78
private void insertExceptionExample () {
77
79
// begin insertExceptionExample
78
80
try {
81
+ // Create a List that will store the bulk operations
79
82
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
80
83
84
+ // Create an InsertOneModel for two documents with ID values of 1 and 3
81
85
InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
82
86
InsertOneModel <Document > doc3 = new InsertOneModel <>(new Document ("_id" , 3 ));
83
87
88
+ // Add the InsertOneModel instances to the bulkOperations list
84
89
bulkOperations .add (doc1 );
85
90
bulkOperations .add (doc3 );
86
91
92
+ // Run the bulk operations on your collection
87
93
collection .bulkWrite (bulkOperations );
88
94
95
+ // Handle any exceptions that occur during the operations
89
96
} catch (MongoBulkWriteException e ){
90
97
System .out .println ("A MongoBulkWriteException occured with the following message: " + e .getMessage ());
91
98
}
92
99
//end insertExceptionExample
93
100
}
94
101
95
102
private void bulkWriteNotOrderedExample () {
103
+ // Create a List that will store the bulk operations
96
104
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
97
105
98
-
106
+ // Create an InsertOneModel for a document with a "name" value of "Zaynab Omar"
99
107
InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
100
108
.append ("name" , "Zaynab Omar" )
101
109
.append ("age" , 37 ));
110
+
111
+ // Create a ReplaceOneModel for a document with a "name" value of "Sandy Kane"
102
112
ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
103
113
new Document ("name" , "Sandy Kane" )
104
- .append ("location" , "Helena, MT" ));
114
+ .append ("location" , "Helena, MT" ));
115
+
116
+ // Create an UpdateOneModel to change the "name" value of a document
105
117
UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
106
118
Updates .set ("name" , "Zaynab Hassan" ));
119
+
120
+ // Create a DeleteManyModel that matches documents with an "age" value greater than 50
107
121
DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
108
122
123
+ // Add each model instance to the bulkOperations list
109
124
bulkOperations .add (insertDoc );
110
125
bulkOperations .add (replaceDoc );
111
126
bulkOperations .add (updateDoc );
112
127
bulkOperations .add (deleteDoc );
113
128
114
-
129
+ // Instruct the driver to execute the bulk operations in any order
115
130
// begin bulkWriteNotOrderedExample
116
131
BulkWriteOptions options = new BulkWriteOptions ().ordered (false );
117
132
133
+ // Run the bulk operations on your collection with an options parameter
118
134
collection .bulkWrite (bulkOperations , options );
119
135
//end bulkWriteNotOrderedExample
120
136
}
121
137
122
138
private void bulkWriteExample () {
139
+ // Create a List that will store the bulk operations
123
140
// begin bulkWriteExample
124
-
125
141
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
126
142
127
143
144
+ // Create an InsertOneModel for a document with a "name" value of "Zaynab Omar"
128
145
InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
129
- .append ("name" , "Zaynab Omar" )
130
- .append ("age" , 37 ));
146
+ .append ("name" , "Zaynab Omar" )
147
+ .append ("age" , 37 ));
148
+
149
+ // Create a ReplaceOneModel for a document with a "name" value of "Sandy Kane"
131
150
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" ),
151
+ new Document ("name" , "Sandy Kane" )
152
+ .append ("location" , "Helena, MT" ));
153
+
154
+ // Create an UpdateOneModel to change the "name" value of a document
155
+ UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
135
156
Updates .set ("name" , "Zaynab Hassan" ));
157
+
158
+ // Create a DeleteManyModel that matches documents with an "age" value greater than 50
136
159
DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
137
160
161
+ // Add each model instance to the bulkOperations list
138
162
bulkOperations .add (insertDoc );
139
163
bulkOperations .add (replaceDoc );
140
164
bulkOperations .add (updateDoc );
141
165
bulkOperations .add (deleteDoc );
142
166
167
+ // Run the bulk operations on your collection
143
168
collection .bulkWrite (bulkOperations );
144
169
//end bulkWriteExample
145
170
}
146
171
147
172
private void insertDocumentsExample (){
173
+ // Create a List that will store the bulk operations
148
174
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
149
175
150
176
// begin insertDocumentsExample
177
+ // Create an InsertOneModel for a document with a "name" value of "June Carrie"
151
178
InsertOneModel <Document > juneDoc = new InsertOneModel <>(new Document ("name" , "June Carrie" )
152
179
.append ("age" , 17 ));
180
+
181
+ // Create an InsertOneModel for a document with a "name" value of "Kevin Moss"
153
182
InsertOneModel <Document > kevinDoc = new InsertOneModel <>(new Document ("name" , "Kevin Moss" )
154
183
.append ("age" , 22 ));
155
184
//end insertDocumentsExample
156
185
186
+ // Add each model instance to the bulkOperations list
157
187
bulkOperations .add (juneDoc );
158
188
bulkOperations .add (kevinDoc );
159
189
190
+ // Run the bulk operations on your collection
160
191
collection .bulkWrite (bulkOperations );
161
192
}
162
193
163
194
private void replaceDocumentsExample (){
195
+ // Create a List that will store the bulk operations
164
196
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
165
197
198
+ // Create a ReplaceOneModel to replace a document with a "_id" value of 1
166
199
// begin replaceDocumentsExample
167
200
ReplaceOneModel <Document > celineDoc = new ReplaceOneModel <>(
168
201
Filters .eq ("_id" , 1 ),
169
202
new Document ("name" , "Celine Stork" )
170
203
.append ("location" , "San Diego, CA" ));
171
204
//end replaceDocumentsExample
172
205
206
+ // Add the ReplaceOneModel instance to the bulkOperations list
173
207
bulkOperations .add (celineDoc );
174
208
209
+ // Run the replace operation on your collection
175
210
collection .bulkWrite (bulkOperations );
176
211
}
177
212
178
213
private void updateDocumentsExample (){
214
+ // Create a List that will store the bulk operations
179
215
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
180
216
217
+ // Create an UpdateOneModel to modify a document with a "_id" value of 2
181
218
// begin updateDocumentsExample
182
219
UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(
183
220
Filters .eq ("_id" , 2 ),
184
221
Updates .set ("age" , 31 ));
185
222
//end updateDocumentsExample
186
223
224
+ // Add the UpdateOneModel instance to the bulkOperations list
187
225
bulkOperations .add (updateDoc );
188
226
227
+ // Run the update operation on your collection
189
228
collection .bulkWrite (bulkOperations );
190
229
}
191
230
192
231
private void deleteDocumentsExample (){
232
+ // Create a List that will store the bulk operations
193
233
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
194
234
235
+ // Create a DeleteOneModel to delete a document with a "_id" value of 1
195
236
// begin deleteDocumentsExample
196
237
DeleteOneModel <Document > deleteDoc = new DeleteOneModel <>(Filters .eq ("_id" , 1 ));
197
238
//end deleteDocumentsExample
198
239
240
+ // Add the DeleteOneModel instance to the bulkOperations list
199
241
bulkOperations .add (deleteDoc );
200
242
243
+ // Run the delete operation on your collection
201
244
collection .bulkWrite (bulkOperations );
202
245
}
203
246
204
247
private void preview (){
248
+ // Print the JSON representation of the returned documents
205
249
collection .find ().forEach (doc -> System .out .println (doc .toJson ()));
206
250
}
207
251
208
252
private void setUpCollection (){
253
+ // Delete the collection so each operation starts with an empty collection
209
254
collection .drop ();
210
255
256
+ // Create a List that will store the bulk operations
211
257
//begin bulkOpsList
212
258
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
213
259
//end bulkOpsList
214
260
261
+ // Create an InsertOneModel for a document with a "name" value of "Karen Sandoval"
215
262
InsertOneModel <Document > karen = new InsertOneModel <>(new Document ("_id" , 1 )
216
263
.append ("name" , "Karen Sandoval" )
217
264
.append ("age" , 31 ));
265
+
266
+ // Create an InsertOneModel for a document with a "name" value of "William Chin"
218
267
InsertOneModel <Document > william = new InsertOneModel <>(new Document ("_id" , 2 )
219
268
.append ("name" , "William Chin" )
220
269
.append ("age" , 54 ));
270
+
271
+ // Create an InsertOneModel for a document with a "name" value of "Shayla Ray"
221
272
InsertOneModel <Document > shayla = new InsertOneModel <>(new Document ("_id" , 8 )
222
273
.append ("name" , "Shayla Ray" )
223
274
.append ("age" , 20 ));
224
-
275
+
276
+ // Add the model instances to the bulkOperations list
225
277
bulkOperations .add (karen );
226
278
bulkOperations .add (william );
227
279
bulkOperations .add (shayla );
228
280
229
-
281
+ // Run the bulk operations on your collection
230
282
collection .bulkWrite (bulkOperations );
231
283
}
232
284
}
0 commit comments