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