@@ -52,18 +52,22 @@ public static void main(String[] args) {
52
52
bulkWrite .insertExceptionExample ();
53
53
54
54
System .out .println ("Insert" );
55
+ bulkWrite .setUpCollection ();
55
56
bulkWrite .insertDocumentsExample ();
56
57
bulkWrite .preview ();
57
58
58
59
System .out .println ("Replace" );
60
+ bulkWrite .setUpCollection ();
59
61
bulkWrite .replaceDocumentsExample ();
60
62
bulkWrite .preview ();
61
63
62
64
System .out .println ("Update" );
65
+ bulkWrite .setUpCollection ();
63
66
bulkWrite .updateDocumentsExample ();
64
67
bulkWrite .preview ();
65
68
66
69
System .out .println ("Delete" );
70
+ bulkWrite .setUpCollection ();
67
71
bulkWrite .deleteDocumentsExample ();
68
72
bulkWrite .preview ();
69
73
}
@@ -91,16 +95,18 @@ private void insertExceptionExample() {
91
95
private void bulkWriteNotOrderedExample () {
92
96
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
93
97
94
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 3 ));
95
- ReplaceOneModel <Document > doc2 = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
96
- new Document ("_id" , 1 ).append ("x" , 2 ));
97
- UpdateOneModel <Document > doc3 = new UpdateOneModel <>(Filters .eq ("_id" , 3 ), Updates .set ("x" , 2 ));
98
- DeleteManyModel <Document > doc4 = new DeleteManyModel <>(Filters .eq ("x" , 2 ));
98
+
99
+ InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 ).append ("name" , "Zaynab Omar" ).append ("age" , 37 ));
100
+ ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
101
+ new Document ("name" , "Sandy Kane" ).append ("location" , "Helena, MT" ));
102
+ UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ), Updates .set ("name" , "Zaynab Hassan" ));
103
+ DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
99
104
100
- bulkOperations .add (doc1 );
101
- bulkOperations .add (doc2 );
102
- bulkOperations .add (doc3 );
103
- bulkOperations .add (doc4 );
105
+ bulkOperations .add (insertDoc );
106
+ bulkOperations .add (replaceDoc );
107
+ bulkOperations .add (updateDoc );
108
+ bulkOperations .add (deleteDoc );
109
+
104
110
105
111
// begin bulkWriteNotOrderedExample
106
112
BulkWriteOptions options = new BulkWriteOptions ().ordered (false );
@@ -114,7 +120,8 @@ private void bulkWriteExample() {
114
120
115
121
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
116
122
117
- InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("name" , "Zaynab Omar" ).append ("age" , 37 ));
123
+
124
+ InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 ).append ("name" , "Zaynab Omar" ).append ("age" , 37 ));
118
125
ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
119
126
new Document ("name" , "Sandy Kane" ).append ("location" , "Helena, MT" ));
120
127
UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ), Updates .set ("name" , "Zaynab Hassan" ));
@@ -130,7 +137,6 @@ private void bulkWriteExample() {
130
137
}
131
138
132
139
private void insertDocumentsExample (){
133
- collection .drop ();
134
140
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
135
141
136
142
// begin insertDocumentsExample
@@ -140,65 +146,47 @@ private void insertDocumentsExample(){
140
146
.append ("age" , 22 ));
141
147
//end insertDocumentsExample
142
148
143
- bulkOperations .add (doc1 );
144
- bulkOperations .add (doc2 );
149
+ bulkOperations .add (juneDoc );
150
+ bulkOperations .add (kevinDoc );
145
151
146
152
collection .bulkWrite (bulkOperations );
147
153
}
148
154
149
155
private void replaceDocumentsExample (){
150
- collection .drop ();
151
156
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
152
157
153
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
154
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
155
-
156
158
// begin replaceDocumentsExample
157
159
ReplaceOneModel <Document > celineDoc = new ReplaceOneModel <>(
158
160
Filters .eq ("_id" , 1 ),
159
161
new Document ("name" , "Celine Stork" ).append ("location" , "San Diego, CA" ));
160
162
//end replaceDocumentsExample
161
163
162
- bulkOperations .add (doc1 );
163
- bulkOperations .add (doc2 );
164
- bulkOperations .add (doc3 );
164
+ bulkOperations .add (celineDoc );
165
165
166
166
collection .bulkWrite (bulkOperations );
167
167
}
168
168
169
169
private void updateDocumentsExample (){
170
- collection .drop ();
171
170
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
172
171
173
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
174
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
175
-
176
172
// begin updateDocumentsExample
177
173
UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(
178
174
Filters .eq ("_id" , 2 ),
179
175
Updates .set ("age" , 31 ));
180
176
//end updateDocumentsExample
181
177
182
- bulkOperations .add (doc1 );
183
- bulkOperations .add (doc2 );
184
- bulkOperations .add (doc3 );
178
+ bulkOperations .add (updateDoc );
185
179
186
180
collection .bulkWrite (bulkOperations );
187
181
}
188
182
189
183
private void deleteDocumentsExample (){
190
- collection .drop ();
191
184
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
192
185
193
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
194
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
195
-
196
186
// begin deleteDocumentsExample
197
187
DeleteOneModel <Document > deleteDoc = new DeleteOneModel <>(Filters .eq ("_id" , 1 ));
198
188
//end deleteDocumentsExample
199
189
200
- bulkOperations .add (doc1 );
201
- bulkOperations .add (doc2 );
202
190
bulkOperations .add (deleteDoc );
203
191
204
192
collection .bulkWrite (bulkOperations );
@@ -215,11 +203,14 @@ private void setUpCollection(){
215
203
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
216
204
//end bulkOpsList
217
205
218
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
219
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
206
+ InsertOneModel <Document > karen = new InsertOneModel <>(new Document ("_id" , 1 ).append ("name" , "Karen Sandoval" ).append ("age" , 31 ));
207
+ InsertOneModel <Document > william = new InsertOneModel <>(new Document ("_id" , 2 ).append ("name" , "William Chin" ).append ("age" , 54 ));
208
+ InsertOneModel <Document > shayla = new InsertOneModel <>(new Document ("_id" , 8 ).append ("name" , "Shayla Ray" ).append ("age" , 20 ));
220
209
221
- bulkOperations .add (doc1 );
222
- bulkOperations .add (doc2 );
210
+ bulkOperations .add (karen );
211
+ bulkOperations .add (william );
212
+ bulkOperations .add (shayla );
213
+
223
214
224
215
collection .bulkWrite (bulkOperations );
225
216
}
0 commit comments