@@ -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
}
@@ -74,11 +78,11 @@ private void insertExceptionExample() {
74
78
try {
75
79
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
76
80
77
- InsertOneModel <Document > doc3 = new InsertOneModel <>(new Document ("_id" , 1 ));
78
- InsertOneModel <Document > doc4 = new InsertOneModel <>(new Document ("_id" , 3 ));
81
+ InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
82
+ InsertOneModel <Document > doc3 = new InsertOneModel <>(new Document ("_id" , 3 ));
79
83
84
+ bulkOperations .add (doc1 );
80
85
bulkOperations .add (doc3 );
81
- bulkOperations .add (doc4 );
82
86
83
87
collection .bulkWrite (bulkOperations );
84
88
@@ -91,16 +95,22 @@ 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 )
100
+ .append ("name" , "Zaynab Omar" )
101
+ .append ("age" , 37 ));
102
+ ReplaceOneModel <Document > replaceDoc = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
103
+ new Document ("name" , "Sandy Kane" )
104
+ .append ("location" , "Helena, MT" ));
105
+ UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(Filters .eq ("name" , "Zaynab Omar" ),
106
+ Updates .set ("name" , "Zaynab Hassan" ));
107
+ DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
99
108
100
- bulkOperations .add (doc1 );
101
- bulkOperations .add (doc2 );
102
- bulkOperations .add (doc3 );
103
- bulkOperations .add (doc4 );
109
+ bulkOperations .add (insertDoc );
110
+ bulkOperations .add (replaceDoc );
111
+ bulkOperations .add (updateDoc );
112
+ bulkOperations .add (deleteDoc );
113
+
104
114
105
115
// begin bulkWriteNotOrderedExample
106
116
BulkWriteOptions options = new BulkWriteOptions ().ordered (false );
@@ -114,90 +124,79 @@ private void bulkWriteExample() {
114
124
115
125
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
116
126
117
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 3 ));
118
- ReplaceOneModel <Document > doc2 = new ReplaceOneModel <>(Filters .eq ("_id" , 1 ),
119
- new Document ("_id" , 1 ).append ("x" , 2 ));
120
- UpdateOneModel <Document > doc3 = new UpdateOneModel <>(Filters .eq ("_id" , 3 ), Updates .set ("x" , 2 ));
121
- DeleteManyModel <Document > doc4 = new DeleteManyModel <>(Filters .eq ("x" , 2 ));
127
+
128
+ InsertOneModel <Document > insertDoc = new InsertOneModel <>(new Document ("_id" , 6 )
129
+ .append ("name" , "Zaynab Omar" )
130
+ .append ("age" , 37 ));
131
+ 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" ),
135
+ Updates .set ("name" , "Zaynab Hassan" ));
136
+ DeleteManyModel <Document > deleteDoc = new DeleteManyModel <>(Filters .gt ("age" , 50 ));
122
137
123
- bulkOperations .add (doc1 );
124
- bulkOperations .add (doc2 );
125
- bulkOperations .add (doc3 );
126
- bulkOperations .add (doc4 );
138
+ bulkOperations .add (insertDoc );
139
+ bulkOperations .add (replaceDoc );
140
+ bulkOperations .add (updateDoc );
141
+ bulkOperations .add (deleteDoc );
127
142
128
143
collection .bulkWrite (bulkOperations );
129
144
//end bulkWriteExample
130
145
}
131
146
132
147
private void insertDocumentsExample (){
133
- collection .drop ();
134
148
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
135
149
136
150
// begin insertDocumentsExample
137
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 3 ));
138
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 4 ));
151
+ InsertOneModel <Document > juneDoc = new InsertOneModel <>(new Document ("name" , "June Carrie" )
152
+ .append ("age" , 17 ));
153
+ InsertOneModel <Document > kevinDoc = new InsertOneModel <>(new Document ("name" , "Kevin Moss" )
154
+ .append ("age" , 22 ));
139
155
//end insertDocumentsExample
140
156
141
- bulkOperations .add (doc1 );
142
- bulkOperations .add (doc2 );
157
+ bulkOperations .add (juneDoc );
158
+ bulkOperations .add (kevinDoc );
143
159
144
160
collection .bulkWrite (bulkOperations );
145
161
}
146
162
147
163
private void replaceDocumentsExample (){
148
- collection .drop ();
149
164
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
150
165
151
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
152
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
153
-
154
166
// begin replaceDocumentsExample
155
- ReplaceOneModel <Document > doc3 = new ReplaceOneModel <>(
167
+ ReplaceOneModel <Document > celineDoc = new ReplaceOneModel <>(
156
168
Filters .eq ("_id" , 1 ),
157
- new Document ("_id" , 1 ).append ("x" , 4 ));
169
+ new Document ("name" , "Celine Stork" )
170
+ .append ("location" , "San Diego, CA" ));
158
171
//end replaceDocumentsExample
159
172
160
- bulkOperations .add (doc1 );
161
- bulkOperations .add (doc2 );
162
- bulkOperations .add (doc3 );
173
+ bulkOperations .add (celineDoc );
163
174
164
175
collection .bulkWrite (bulkOperations );
165
176
}
166
177
167
178
private void updateDocumentsExample (){
168
- collection .drop ();
169
179
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
170
180
171
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
172
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
173
-
174
181
// begin updateDocumentsExample
175
- UpdateOneModel <Document > doc3 = new UpdateOneModel <>(
182
+ UpdateOneModel <Document > updateDoc = new UpdateOneModel <>(
176
183
Filters .eq ("_id" , 2 ),
177
- Updates .set ("x " , 8 ));
184
+ Updates .set ("age " , 31 ));
178
185
//end updateDocumentsExample
179
186
180
- bulkOperations .add (doc1 );
181
- bulkOperations .add (doc2 );
182
- bulkOperations .add (doc3 );
187
+ bulkOperations .add (updateDoc );
183
188
184
189
collection .bulkWrite (bulkOperations );
185
190
}
186
191
187
192
private void deleteDocumentsExample (){
188
- collection .drop ();
189
193
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
190
194
191
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
192
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
193
-
194
195
// begin deleteDocumentsExample
195
- DeleteOneModel <Document > doc3 = new DeleteOneModel <>(Filters .eq ("_id" , 1 ));
196
+ DeleteOneModel <Document > deleteDoc = new DeleteOneModel <>(Filters .eq ("_id" , 1 ));
196
197
//end deleteDocumentsExample
197
198
198
- bulkOperations .add (doc1 );
199
- bulkOperations .add (doc2 );
200
- bulkOperations .add (doc3 );
199
+ bulkOperations .add (deleteDoc );
201
200
202
201
collection .bulkWrite (bulkOperations );
203
202
}
@@ -213,11 +212,20 @@ private void setUpCollection(){
213
212
List <WriteModel <Document >> bulkOperations = new ArrayList <>();
214
213
//end bulkOpsList
215
214
216
- InsertOneModel <Document > doc1 = new InsertOneModel <>(new Document ("_id" , 1 ));
217
- InsertOneModel <Document > doc2 = new InsertOneModel <>(new Document ("_id" , 2 ));
215
+ InsertOneModel <Document > karen = new InsertOneModel <>(new Document ("_id" , 1 )
216
+ .append ("name" , "Karen Sandoval" )
217
+ .append ("age" , 31 ));
218
+ InsertOneModel <Document > william = new InsertOneModel <>(new Document ("_id" , 2 )
219
+ .append ("name" , "William Chin" )
220
+ .append ("age" , 54 ));
221
+ InsertOneModel <Document > shayla = new InsertOneModel <>(new Document ("_id" , 8 )
222
+ .append ("name" , "Shayla Ray" )
223
+ .append ("age" , 20 ));
218
224
219
- bulkOperations .add (doc1 );
220
- bulkOperations .add (doc2 );
225
+ bulkOperations .add (karen );
226
+ bulkOperations .add (william );
227
+ bulkOperations .add (shayla );
228
+
221
229
222
230
collection .bulkWrite (bulkOperations );
223
231
}
0 commit comments