Skip to content

Commit 1359e43

Browse files
committed
DOCSP-33345: Java comments pt. 4 (#472)
* DOCSP-33345: Java code comments pt. 4 * bulkwrite CC feedback * applying CC feedback * small edit * half of feedback * rest of feedback * single line comment * duplicate comments sheet suggestions * small edit * final feedback (cherry picked from commit 4b9070c) (cherry picked from commit fd983e9)
1 parent f58ac98 commit 1359e43

File tree

5 files changed

+163
-28
lines changed

5 files changed

+163
-28
lines changed

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

Lines changed: 9 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

@@ -30,6 +31,8 @@ public static void main(String[] args) {
3031
// end connection
3132

3233
collection.drop();
34+
35+
// Inserts sample documents describing restaurants
3336
// begin insert
3437
collection.insertMany(Arrays.asList(
3538
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,32 +48,37 @@ public static void main(String[] args) {
4548
));
4649
// end insert
4750

51+
// Creates an aggregation pipeline that matches documents, groups them by the "stars" field, and tallies them by distinct values
4852
// begin aggregation one
4953
collection.aggregate(
5054
Arrays.asList(
5155
Aggregates.match(Filters.eq("categories", "Bakery")),
5256
Aggregates.group("$stars", Accumulators.sum("count", 1))
5357
)
58+
// Prints the result of the aggregation operation as JSON
5459
).forEach(doc -> System.out.println(doc.toJson()));
5560
// end aggregation one
61+
5662
// begin aggregation three
5763
Document explanation = collection.aggregate(
5864
Arrays.asList(
59-
Aggregates.match(Filters.eq("categories", "bakery")),
65+
Aggregates.match(Filters.eq("categories", "Bakery")),
6066
Aggregates.group("$stars", Accumulators.sum("count", 1))
6167
)
6268
).explain(ExplainVerbosity.EXECUTION_STATS);
6369

6470
List<Document> stages = explanation.get("stages", List.class);
6571
List<String> keys = Arrays.asList("queryPlanner", "winningPlan");
6672

73+
// Prints the JSON representation of the winning execution plans
6774
for (Document stage : stages) {
6875
Document cursorStage = stage.get("$cursor", Document.class);
6976
if (cursorStage != null) {
7077
System.out.println(cursorStage.getEmbedded(keys, Document.class).toJson());
7178
}
7279
}
7380
// end aggregation three
81+
// Prints the restaurant name and the first value in the "categories" array as a field named "firstCategory"
7482
// begin aggregation two
7583
collection.aggregate(
7684
Arrays.asList(

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

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,68 +73,84 @@ private void insertExceptionExample() {
7373
// begin insertExceptionExample
7474
try {
7575
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
76-
76+
77+
// Creates instructions to insert documents
7778
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
7879
InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 3));
7980

8081
bulkOperations.add(doc1);
8182
bulkOperations.add(doc3);
8283

84+
// Runs a bulk write operation for the specified insert WriteModels
8385
collection.bulkWrite(bulkOperations);
8486

87+
// Prints a message if any exceptions occur during the bulk write operation
8588
} catch (MongoBulkWriteException e){
86-
System.out.println("A MongoBulkWriteException occured with the following message: " + e.getMessage());
89+
System.out.println("A MongoBulkWriteException occurred with the following message: " + e.getMessage());
8790
}
8891
//end insertExceptionExample
8992
}
9093

9194
private void bulkWriteNotOrderedExample() {
9295
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
9396

94-
97+
// Creates instructions to insert a document
9598
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
9699
.append("name", "Zaynab Omar")
97100
.append("age", 37));
101+
102+
// Creates instructions to replace the first document that matches the query
98103
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
99104
new Document("name", "Sandy Kane")
100-
.append("location", "Helena, MT"));
105+
.append("location", "Helena, MT"));
106+
107+
// Creates instructions to update the first document that matches the query
101108
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
102109
Updates.set("name", "Zaynab Hassan"));
110+
111+
// Creates instructions to delete all documents that match the query
103112
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
104113

105-
bulkOperations.add(doc1);
106-
bulkOperations.add(doc2);
107-
bulkOperations.add(doc3);
108-
bulkOperations.add(doc4);
109-
114+
bulkOperations.add(insertDoc);
115+
bulkOperations.add(replaceDoc);
116+
bulkOperations.add(updateDoc);
117+
bulkOperations.add(deleteDoc);
118+
110119
// begin bulkWriteNotOrderedExample
111120
BulkWriteOptions options = new BulkWriteOptions().ordered(false);
112121

122+
// Runs a bulk write operation for the specified insert, replace, update, and delete WriteModels in any order
113123
collection.bulkWrite(bulkOperations, options);
114124
//end bulkWriteNotOrderedExample
115125
}
116126

117127
private void bulkWriteExample() {
118128
// begin bulkWriteExample
119-
120129
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
121130

122-
131+
// Creates instructions to insert a document
123132
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
124-
.append("name", "Zaynab Omar")
125-
.append("age", 37));
133+
.append("name", "Zaynab Omar")
134+
.append("age", 37));
135+
136+
// Creates instructions to replace the first document matched by the query
126137
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
127-
new Document("name", "Sandy Kane")
128-
.append("location", "Helena, MT"));
129-
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
138+
new Document("name", "Sandy Kane")
139+
.append("location", "Helena, MT"));
140+
141+
// Creates instructions to update the first document matched by the query
142+
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
130143
Updates.set("name", "Zaynab Hassan"));
144+
145+
// Creates instructions to delete all documents matched by the query
131146
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
132147

133148
bulkOperations.add(doc1);
134149
bulkOperations.add(doc2);
135150
bulkOperations.add(doc3);
136151
bulkOperations.add(doc4);
137152

153+
// Runs a bulk write operation for the specified the insert, replace, update, and delete WriteModels in order
138154
collection.bulkWrite(bulkOperations);
139155
//end bulkWriteExample
140156
}
@@ -143,14 +159,19 @@ private void insertDocumentsExample(){
143159
collection.drop();
144160
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
145161

162+
// Creates instructions to insert multiple documents
146163
// begin insertDocumentsExample
147-
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 3));
148-
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 4));
164+
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie")
165+
.append("age", 17));
166+
167+
InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss")
168+
.append("age", 22));
149169
//end insertDocumentsExample
150170

151171
bulkOperations.add(doc1);
152172
bulkOperations.add(doc2);
153173

174+
// Runs a bulk write operation for the specified insert WriteModels
154175
collection.bulkWrite(bulkOperations);
155176
}
156177

@@ -161,6 +182,7 @@ private void replaceDocumentsExample(){
161182
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
162183
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
163184

185+
// Creates instructions to replace the first document matched by the query
164186
// begin replaceDocumentsExample
165187
ReplaceOneModel<Document> doc3 = new ReplaceOneModel<>(
166188
Filters.eq("_id", 1),
@@ -172,6 +194,7 @@ private void replaceDocumentsExample(){
172194
bulkOperations.add(doc2);
173195
bulkOperations.add(doc3);
174196

197+
// Runs a bulk write operation for the specified replace WriteModel
175198
collection.bulkWrite(bulkOperations);
176199
}
177200

@@ -182,6 +205,7 @@ private void updateDocumentsExample(){
182205
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
183206
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
184207

208+
// Creates instructions to update the first document matched by the query
185209
// begin updateDocumentsExample
186210
UpdateOneModel<Document> doc3 = new UpdateOneModel<>(
187211
Filters.eq("_id", 2),
@@ -192,6 +216,7 @@ private void updateDocumentsExample(){
192216
bulkOperations.add(doc2);
193217
bulkOperations.add(doc3);
194218

219+
// Runs a bulk write operation for the specified update WriteModel
195220
collection.bulkWrite(bulkOperations);
196221
}
197222

@@ -202,6 +227,7 @@ private void deleteDocumentsExample(){
202227
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
203228
InsertOneModel<Document> doc2 = new InsertOneModel<>(new Document("_id", 2));
204229

230+
// Creates instructions to delete the first document matched by the query
205231
// begin deleteDocumentsExample
206232
DeleteOneModel<Document> doc3 = new DeleteOneModel<>(Filters.eq("_id", 1));
207233
//end deleteDocumentsExample
@@ -210,6 +236,7 @@ private void deleteDocumentsExample(){
210236
bulkOperations.add(doc2);
211237
bulkOperations.add(doc3);
212238

239+
// Runs a bulk write operation for the specified delete WriteModel
213240
collection.bulkWrite(bulkOperations);
214241
}
215242

@@ -227,15 +254,18 @@ private void setUpCollection(){
227254
InsertOneModel<Document> karen = new InsertOneModel<>(new Document("_id", 1)
228255
.append("name", "Karen Sandoval")
229256
.append("age", 31));
257+
230258
InsertOneModel<Document> william = new InsertOneModel<>(new Document("_id", 2)
231259
.append("name", "William Chin")
232260
.append("age", 54));
261+
233262
InsertOneModel<Document> shayla = new InsertOneModel<>(new Document("_id", 8)
234263
.append("name", "Shayla Ray")
235264
.append("age", 20));
236-
237-
bulkOperations.add(doc1);
238-
bulkOperations.add(doc2);
265+
266+
bulkOperations.add(karen);
267+
bulkOperations.add(william);
268+
bulkOperations.add(shayla);
239269

240270
collection.bulkWrite(bulkOperations);
241271
}

0 commit comments

Comments
 (0)