Skip to content

Commit ce57a6d

Browse files
committed
DOCSP-33345: Java code comments pt. 4
1 parent b8c6fa7 commit ce57a6d

File tree

5 files changed

+196
-15
lines changed

5 files changed

+196
-15
lines changed

source/includes/fundamentals/code-snippets/AggTour.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.bson.Document;
1515

1616
import java.util.Arrays;
17+
import java.util.List;
1718

1819
// end imports
1920

@@ -24,12 +25,16 @@ public static void main(String[] args) {
2425
// Replace the uri string with your MongoDB deployment's connection string
2526
final String uri = "<connection string uri>";
2627

28+
// Create a client and access the "restaurants" collection in the "aggregation" database
2729
MongoClient mongoClient = MongoClients.create(uri);
2830
MongoDatabase database = mongoClient.getDatabase("aggregation");
2931
MongoCollection<Document> collection = database.getCollection("restaurants");
3032
// end connection
3133

34+
// Drop the collection, if it exists, to ensure the collection initally does not contain any documents
3235
collection.drop();
36+
37+
// Insert sample restaurant documents
3338
// begin insert
3439
collection.insertMany(Arrays.asList(
3540
new Document("name", "Sun Bakery Trattoria").append("contact", new Document().append("phone", "386-555-0189").append("email", "[email protected]").append("location", Arrays.asList(-74.0056649, 40.7452371))).append("stars", 4).append("categories", Arrays.asList("Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")),
@@ -45,6 +50,8 @@ public static void main(String[] args) {
4550
));
4651
// end insert
4752

53+
// Filter for documents whose "categories" array field contains "Bakery"
54+
// Then, group documents by the "stars" field and accumulate a count of distinct "stars" values
4855
// begin aggregation one
4956
collection.aggregate(
5057
Arrays.asList(
@@ -53,24 +60,29 @@ public static void main(String[] args) {
5360
)
5461
).forEach(doc -> System.out.println(doc.toJson()));
5562
// end aggregation one
63+
64+
// Use the "explain()" method to see the operation's execution plans and performance statistics
5665
// begin aggregation three
5766
Document explanation = collection.aggregate(
5867
Arrays.asList(
59-
Aggregates.match(Filters.eq("categories", "bakery")),
68+
Aggregates.match(Filters.eq("categories", "Bakery")),
6069
Aggregates.group("$stars", Accumulators.sum("count", 1))
6170
)
6271
).explain(ExplainVerbosity.EXECUTION_STATS);
6372

73+
// Obtain the winning plans for aggregation stages that produce execution plans
6474
List<Document> stages = explanation.get("stages", List.class);
6575
List<String> keys = Arrays.asList("queryPlanner", "winningPlan");
6676

77+
// Print the JSON representation of the winning plans
6778
for (Document stage : stages) {
6879
Document cursorStage = stage.get("$cursor", Document.class);
6980
if (cursorStage != null) {
7081
System.out.println(cursorStage.getEmbedded(keys, Document.class).toJson());
7182
}
7283
}
7384
// end aggregation three
85+
// Use a $project stage to return the "name" field and the calculated field "firstCategory"
7486
// begin aggregation two
7587
collection.aggregate(
7688
Arrays.asList(

source/includes/fundamentals/code-snippets/BulkWrite.java

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public class BulkWrite {
3030
private BulkWrite() {
3131
final String uri = System.getenv("DRIVER_REF_URI");
3232

33+
// Create a client and access the "bulkWrite" collection in the "crudOps" database
3334
mongoClient = MongoClients.create(uri);
3435
database = mongoClient.getDatabase("crudOps");
3536
collection = database.getCollection("bulkWrite");
3637
}
3738

3839
public static void main(String[] args) {
40+
// For each operation, set up the collection, run the operation, and print the results
3941
BulkWrite bulkWrite = new BulkWrite();
4042
System.out.println("Ordered BulkWrite");
4143
bulkWrite.setUpCollection();
@@ -76,157 +78,207 @@ public static void main(String[] args) {
7678
private void insertExceptionExample() {
7779
// begin insertExceptionExample
7880
try {
81+
// Create a List that will store the bulk operations
7982
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
8083

84+
// Create an InsertOneModel for two documents with ID values of 1 and 3
8185
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
8286
InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 3));
8387

88+
// Add the InsertOneModel instances to the bulkOperations list
8489
bulkOperations.add(doc1);
8590
bulkOperations.add(doc3);
8691

92+
// Run the bulk operations on your collection
8793
collection.bulkWrite(bulkOperations);
8894

95+
// Handle any exceptions that occur during the operations
8996
} catch (MongoBulkWriteException e){
9097
System.out.println("A MongoBulkWriteException occured with the following message: " + e.getMessage());
9198
}
9299
//end insertExceptionExample
93100
}
94101

95102
private void bulkWriteNotOrderedExample() {
103+
// Create a List that will store the bulk operations
96104
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
97105

98-
106+
// Create an InsertOneModel for a document with a "name" value of "Zaynab Omar"
99107
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
100108
.append("name", "Zaynab Omar")
101109
.append("age", 37));
110+
111+
// Create a ReplaceOneModel for a document with a "name" value of "Sandy Kane"
102112
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
103113
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
105117
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
106118
Updates.set("name", "Zaynab Hassan"));
119+
120+
// Create a DeleteManyModel that matches documents with an "age" value greater than 50
107121
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
108122

123+
// Add each model instance to the bulkOperations list
109124
bulkOperations.add(insertDoc);
110125
bulkOperations.add(replaceDoc);
111126
bulkOperations.add(updateDoc);
112127
bulkOperations.add(deleteDoc);
113128

114-
129+
// Instruct the driver to execute the bulk operations in any order
115130
// begin bulkWriteNotOrderedExample
116131
BulkWriteOptions options = new BulkWriteOptions().ordered(false);
117132

133+
// Run the bulk operations on your collection with an options parameter
118134
collection.bulkWrite(bulkOperations, options);
119135
//end bulkWriteNotOrderedExample
120136
}
121137

122138
private void bulkWriteExample() {
139+
// Create a List that will store the bulk operations
123140
// begin bulkWriteExample
124-
125141
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
126142

127143

144+
// Create an InsertOneModel for a document with a "name" value of "Zaynab Omar"
128145
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"
131150
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"),
135156
Updates.set("name", "Zaynab Hassan"));
157+
158+
// Create a DeleteManyModel that matches documents with an "age" value greater than 50
136159
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
137160

161+
// Add each model instance to the bulkOperations list
138162
bulkOperations.add(insertDoc);
139163
bulkOperations.add(replaceDoc);
140164
bulkOperations.add(updateDoc);
141165
bulkOperations.add(deleteDoc);
142166

167+
// Run the bulk operations on your collection
143168
collection.bulkWrite(bulkOperations);
144169
//end bulkWriteExample
145170
}
146171

147172
private void insertDocumentsExample(){
173+
// Create a List that will store the bulk operations
148174
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
149175

150176
// begin insertDocumentsExample
177+
// Create an InsertOneModel for a document with a "name" value of "June Carrie"
151178
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie")
152179
.append("age", 17));
180+
181+
// Create an InsertOneModel for a document with a "name" value of "Kevin Moss"
153182
InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss")
154183
.append("age", 22));
155184
//end insertDocumentsExample
156185

186+
// Add each model instance to the bulkOperations list
157187
bulkOperations.add(juneDoc);
158188
bulkOperations.add(kevinDoc);
159189

190+
// Run the bulk operations on your collection
160191
collection.bulkWrite(bulkOperations);
161192
}
162193

163194
private void replaceDocumentsExample(){
195+
// Create a List that will store the bulk operations
164196
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
165197

198+
// Create a ReplaceOneModel to replace a document with a "_id" value of 1
166199
// begin replaceDocumentsExample
167200
ReplaceOneModel<Document> celineDoc = new ReplaceOneModel<>(
168201
Filters.eq("_id", 1),
169202
new Document("name", "Celine Stork")
170203
.append("location", "San Diego, CA"));
171204
//end replaceDocumentsExample
172205

206+
// Add the ReplaceOneModel instance to the bulkOperations list
173207
bulkOperations.add(celineDoc);
174208

209+
// Run the replace operation on your collection
175210
collection.bulkWrite(bulkOperations);
176211
}
177212

178213
private void updateDocumentsExample(){
214+
// Create a List that will store the bulk operations
179215
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
180216

217+
// Create an UpdateOneModel to modify a document with a "_id" value of 2
181218
// begin updateDocumentsExample
182219
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(
183220
Filters.eq("_id", 2),
184221
Updates.set("age", 31));
185222
//end updateDocumentsExample
186223

224+
// Add the UpdateOneModel instance to the bulkOperations list
187225
bulkOperations.add(updateDoc);
188226

227+
// Run the update operation on your collection
189228
collection.bulkWrite(bulkOperations);
190229
}
191230

192231
private void deleteDocumentsExample(){
232+
// Create a List that will store the bulk operations
193233
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
194234

235+
// Create a DeleteOneModel to delete a document with a "_id" value of 1
195236
// begin deleteDocumentsExample
196237
DeleteOneModel<Document> deleteDoc = new DeleteOneModel<>(Filters.eq("_id", 1));
197238
//end deleteDocumentsExample
198239

240+
// Add the DeleteOneModel instance to the bulkOperations list
199241
bulkOperations.add(deleteDoc);
200242

243+
// Run the delete operation on your collection
201244
collection.bulkWrite(bulkOperations);
202245
}
203246

204247
private void preview(){
248+
// Print the JSON representation of the returned documents
205249
collection.find().forEach(doc -> System.out.println(doc.toJson()));
206250
}
207251

208252
private void setUpCollection(){
253+
// Delete the collection so each operation starts with an empty collection
209254
collection.drop();
210255

256+
// Create a List that will store the bulk operations
211257
//begin bulkOpsList
212258
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
213259
//end bulkOpsList
214260

261+
// Create an InsertOneModel for a document with a "name" value of "Karen Sandoval"
215262
InsertOneModel<Document> karen = new InsertOneModel<>(new Document("_id", 1)
216263
.append("name", "Karen Sandoval")
217264
.append("age", 31));
265+
266+
// Create an InsertOneModel for a document with a "name" value of "William Chin"
218267
InsertOneModel<Document> william = new InsertOneModel<>(new Document("_id", 2)
219268
.append("name", "William Chin")
220269
.append("age", 54));
270+
271+
// Create an InsertOneModel for a document with a "name" value of "Shayla Ray"
221272
InsertOneModel<Document> shayla = new InsertOneModel<>(new Document("_id", 8)
222273
.append("name", "Shayla Ray")
223274
.append("age", 20));
224-
275+
276+
// Add the model instances to the bulkOperations list
225277
bulkOperations.add(karen);
226278
bulkOperations.add(william);
227279
bulkOperations.add(shayla);
228280

229-
281+
// Run the bulk operations on your collection
230282
collection.bulkWrite(bulkOperations);
231283
}
232284
}

0 commit comments

Comments
 (0)