1
- package docs ;
1
+ // Runs bulk write operations on a collection by using the Java driver
2
2
3
+ package usage .examples ;
4
+
5
+ import java .util .Arrays ;
6
+
7
+ import org .bson .Document ;
8
+
9
+ import com .mongodb .MongoException ;
10
+ import com .mongodb .bulk .BulkWriteResult ;
3
11
import com .mongodb .client .MongoClient ;
4
12
import com .mongodb .client .MongoClients ;
5
13
import com .mongodb .client .MongoCollection ;
6
14
import com .mongodb .client .MongoDatabase ;
15
+ import com .mongodb .client .model .DeleteOneModel ;
7
16
import com .mongodb .client .model .InsertOneModel ;
8
- import com .mongodb .client .model .UpdateOneModel ;
9
17
import com .mongodb .client .model .ReplaceOneModel ;
10
- import com .mongodb .client .model .BulkWriteOptions ;
11
- import com .mongodb .client .model .DeleteOneModel ;
12
- import com .mongodb .client .model .DeleteManyModel ;
13
-
14
- import com .mongodb .MongoBulkWriteException ;
15
-
16
- import org .bson .Document ;
17
-
18
- import java .util .*;
19
-
20
- import com .mongodb .client .model .Filters ;
21
- import com .mongodb .client .model .Updates ;
22
- import com .mongodb .client .model .WriteModel ;
18
+ import com .mongodb .client .model .UpdateOneModel ;
19
+ import com .mongodb .client .model .UpdateOptions ;
23
20
24
21
public class BulkWrite {
25
-
26
- private final MongoCollection <Document > collection ;
27
- private final MongoClient mongoClient ;
28
- private final MongoDatabase database ;
29
-
30
- private BulkWrite () {
31
- final String uri = System .getenv ("DRIVER_REF_URI" );
32
-
33
- mongoClient = MongoClients .create (uri );
34
- database = mongoClient .getDatabase ("crudOps" );
35
- collection = database .getCollection ("bulkWrite" );
36
- }
37
-
38
22
public static void main (String [] args ) {
39
- BulkWrite bulkWrite = new BulkWrite ();
40
- System .out .println ("Ordered BulkWrite" );
41
- bulkWrite .setUpCollection ();
42
- bulkWrite .bulkWriteExample ();
43
- bulkWrite .preview ();
44
-
45
- System .out .println ("Unordered BulkWrite" );
46
- bulkWrite .setUpCollection ();
47
- bulkWrite .bulkWriteNotOrderedExample ();
48
- bulkWrite .preview ();
49
-
50
- System .out .println ("Insert BulkWriteException" );
51
- bulkWrite .setUpCollection ();
52
- bulkWrite .insertExceptionExample ();
53
-
54
- System .out .println ("Insert" );
55
- bulkWrite .setUpCollection ();
56
- bulkWrite .insertDocumentsExample ();
57
- bulkWrite .preview ();
58
-
59
- System .out .println ("Replace" );
60
- bulkWrite .setUpCollection ();
61
- bulkWrite .replaceDocumentsExample ();
62
- bulkWrite .preview ();
63
-
64
- System .out .println ("Update" );
65
- bulkWrite .setUpCollection ();
66
- bulkWrite .updateDocumentsExample ();
67
- bulkWrite .preview ();
68
-
69
- System .out .println ("Delete" );
70
- bulkWrite .setUpCollection ();
71
- bulkWrite .deleteDocumentsExample ();
72
- bulkWrite .preview ();
73
- }
74
-
75
-
76
- private void insertExceptionExample () {
77
- // begin insertExceptionExample
78
- try {
79
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
80
-
81
- // Creates instructions to insert documents
82
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
83
- InsertOneModel <Document > doc3 = new InsertOneModel <>(new Document ("_id" , 3 ));
84
-
85
- bulkOperations .add (doc1 );
86
- bulkOperations .add (doc3 );
87
-
88
- // Runs a bulk write operation for the specified insert WriteModels
89
- collection .bulkWrite (bulkOperations );
90
-
91
- // Prints a message if any exceptions occur during the bulk write operation
92
- } catch (MongoBulkWriteException e ){
93
- System .out .println ("A MongoBulkWriteException occurred with the following message: " + e .getMessage ());
23
+ // Replace the uri string with your MongoDB deployment's connection string
24
+ String uri = "<connection string uri>" ;
25
+
26
+ try (MongoClient mongoClient = MongoClients .create (uri )) {
27
+ MongoDatabase database = mongoClient .getDatabase ("sample_mflix" );
28
+ MongoCollection <Document > collection = database .getCollection ("movies" );
29
+
30
+ try {
31
+ // Runs a bulk write operation for the specified insert, update, delete, and replace operations
32
+ BulkWriteResult result = collection .bulkWrite (
33
+ Arrays .asList (
34
+ new InsertOneModel <>(new Document ("name" , "A Sample Movie" )),
35
+ new InsertOneModel <>(new Document ("name" , "Another Sample Movie" )),
36
+ new InsertOneModel <>(new Document ("name" , "Yet Another Sample Movie" )),
37
+
38
+ new UpdateOneModel <>(new Document ("name" , "A Sample Movie" ),
39
+ new Document ("$set" , new Document ("name" , "An Old Sample Movie" )),
40
+ new UpdateOptions ().upsert (true )),
41
+
42
+ new DeleteOneModel <>(new Document ("name" , "Yet Another Sample Movie" )),
43
+
44
+ new ReplaceOneModel <>(new Document ("name" , "Yet Another Sample Movie" ),
45
+ new Document ("name" , "The Other Sample Movie" ).append ("runtime" , "42" ))
46
+ ));
47
+ // Prints the number of inserted, updated, and deleted documents
48
+ System .out .println ("Result statistics:" +
49
+ "\n inserted: " + result .getInsertedCount () +
50
+ "\n updated: " + result .getModifiedCount () +
51
+ "\n deleted: " + result .getDeletedCount ());
52
+
53
+ // Prints a message if any exceptions occur during the operations
54
+ } catch (MongoException me ) {
55
+ System .err .println ("The bulk write operation failed due to an error: " + me );
56
+ }
94
57
}
95
- //end insertExceptionExample
96
- }
97
-
98
- private void bulkWriteNotOrderedExample () {
99
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
100
-
101
- // Creates instructions to insert a document
102
- InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
103
- .append ("name" , "Zaynab Omar" )
104
- .append ("age" , 37 ));
105
-
106
- // Creates instructions to replace the first document that matches the query
107
- ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
108
- new Document ("name" , "Sandy Kane" )
109
- .append ("location" , "Helena, MT" ));
110
-
111
- // Creates instructions to update the first document that matches the query
112
- UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
113
- Updates .set ("name" , "Zaynab Hassan" ));
114
-
115
- // Creates instructions to delete all documents that match the query
116
- DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
117
-
118
- bulkOperations .add (insertDoc );
119
- bulkOperations .add (replaceDoc );
120
- bulkOperations .add (updateDoc );
121
- bulkOperations .add (deleteDoc );
122
-
123
- // begin bulkWriteNotOrderedExample
124
- BulkWriteOptions options = new BulkWriteOptions ().ordered (false );
125
-
126
- // Runs a bulk write operation for the specified insert, replace, update, and delete WriteModels in any order
127
- collection .bulkWrite (bulkOperations , options );
128
- //end bulkWriteNotOrderedExample
129
- }
130
-
131
- private void bulkWriteExample () {
132
- // begin bulkWriteExample
133
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
134
-
135
- // Creates instructions to insert a document
136
- InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
137
- .append ("name" , "Zaynab Omar" )
138
- .append ("age" , 37 ));
139
-
140
- // Creates instructions to replace the first document matched by the query
141
- ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
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" ),
147
- Updates .set ("name" , "Zaynab Hassan" ));
148
-
149
- // Creates instructions to delete all documents matched by the query
150
- DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
151
-
152
- bulkOperations .add (insertDoc );
153
- bulkOperations .add (replaceDoc );
154
- bulkOperations .add (updateDoc );
155
- bulkOperations .add (deleteDoc );
156
-
157
- // Runs a bulk write operation for the specified the insert, replace, update, and delete WriteModels in order
158
- collection .bulkWrite (bulkOperations );
159
- //end bulkWriteExample
160
- }
161
-
162
- private void insertDocumentsExample (){
163
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
164
-
165
- // Creates instructions to insert multiple documents
166
- // begin insertDocumentsExample
167
- InsertOneModel <Document > juneDoc = new InsertOneModel <>(new Document ("name" , "June Carrie" )
168
- .append ("age" , 17 ));
169
-
170
- InsertOneModel <Document > kevinDoc = new InsertOneModel <>(new Document ("name" , "Kevin Moss" )
171
- .append ("age" , 22 ));
172
- //end insertDocumentsExample
173
-
174
- bulkOperations .add (juneDoc );
175
- bulkOperations .add (kevinDoc );
176
-
177
- // Runs a bulk write operation for the specified insert WriteModels
178
- collection .bulkWrite (bulkOperations );
179
- }
180
-
181
- private void replaceDocumentsExample (){
182
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
183
-
184
- // Creates instructions to replace the first document matched by the query
185
- // begin replaceDocumentsExample
186
- ReplaceOneModel <Document > celineDoc = new ReplaceOneModel <>(
187
- Filters .eq ("_id" , 1 ),
188
- new Document ("name" , "Celine Stork" )
189
- .append ("location" , "San Diego, CA" ));
190
- //end replaceDocumentsExample
191
-
192
- bulkOperations .add (celineDoc );
193
-
194
- // Runs a bulk write operation for the specified replace WriteModel
195
- collection .bulkWrite (bulkOperations );
196
- }
197
-
198
- private void updateDocumentsExample (){
199
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
200
-
201
- // Creates instructions to update the first document matched by the query
202
- // begin updateDocumentsExample
203
- UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(
204
- Filters .eq ("_id" , 2 ),
205
- Updates .set ("age" , 31 ));
206
- //end updateDocumentsExample
207
-
208
- bulkOperations .add (updateDoc );
209
-
210
- // Runs a bulk write operation for the specified update WriteModel
211
- collection .bulkWrite (bulkOperations );
212
- }
213
-
214
- private void deleteDocumentsExample (){
215
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
216
-
217
- // Creates instructions to delete the first document matched by the query
218
- // begin deleteDocumentsExample
219
- DeleteOneModel <Document > deleteDoc = new DeleteOneModel <>(Filters .eq ("_id" , 1 ));
220
- //end deleteDocumentsExample
221
-
222
- bulkOperations .add (deleteDoc );
223
-
224
- // Runs a bulk write operation for the specified delete WriteModel
225
- collection .bulkWrite (bulkOperations );
226
- }
227
-
228
- private void preview (){
229
- collection .find ().forEach (doc -> System .out .println (doc .toJson ()));
230
- }
231
-
232
- private void setUpCollection (){
233
- collection .drop ();
234
-
235
- //begin bulkOpsList
236
- List <WriteModel <Document >> bulkOperations = new ArrayList <>();
237
- //end bulkOpsList
238
-
239
- InsertOneModel <Document > karen = new InsertOneModel <>(new Document ("_id" , 1 )
240
- .append ("name" , "Karen Sandoval" )
241
- .append ("age" , 31 ));
242
-
243
- InsertOneModel <Document > william = new InsertOneModel <>(new Document ("_id" , 2 )
244
- .append ("name" , "William Chin" )
245
- .append ("age" , 54 ));
246
-
247
- InsertOneModel <Document > shayla = new InsertOneModel <>(new Document ("_id" , 8 )
248
- .append ("name" , "Shayla Ray" )
249
- .append ("age" , 20 ));
250
-
251
- bulkOperations .add (karen );
252
- bulkOperations .add (william );
253
- bulkOperations .add (shayla );
254
-
255
- collection .bulkWrite (bulkOperations );
256
58
}
257
- }
59
+ }
0 commit comments