Skip to content

Commit 4b9070c

Browse files
authored
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
1 parent 6ca9e93 commit 4b9070c

File tree

5 files changed

+151
-21
lines changed

5 files changed

+151
-21
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: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,92 +77,111 @@ private void insertExceptionExample() {
7777
// begin insertExceptionExample
7878
try {
7979
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
80-
80+
81+
// Creates instructions to insert documents
8182
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1));
8283
InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 3));
8384

8485
bulkOperations.add(doc1);
8586
bulkOperations.add(doc3);
8687

88+
// Runs a bulk write operation for the specified insert WriteModels
8789
collection.bulkWrite(bulkOperations);
8890

91+
// Prints a message if any exceptions occur during the bulk write operation
8992
} catch (MongoBulkWriteException e){
90-
System.out.println("A MongoBulkWriteException occured with the following message: " + e.getMessage());
93+
System.out.println("A MongoBulkWriteException occurred with the following message: " + e.getMessage());
9194
}
9295
//end insertExceptionExample
9396
}
9497

9598
private void bulkWriteNotOrderedExample() {
9699
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
97100

98-
101+
// Creates instructions to insert a document
99102
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
100103
.append("name", "Zaynab Omar")
101104
.append("age", 37));
105+
106+
// Creates instructions to replace the first document that matches the query
102107
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1),
103108
new Document("name", "Sandy Kane")
104-
.append("location", "Helena, MT"));
109+
.append("location", "Helena, MT"));
110+
111+
// Creates instructions to update the first document that matches the query
105112
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
106113
Updates.set("name", "Zaynab Hassan"));
114+
115+
// Creates instructions to delete all documents that match the query
107116
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
108117

109118
bulkOperations.add(insertDoc);
110119
bulkOperations.add(replaceDoc);
111120
bulkOperations.add(updateDoc);
112121
bulkOperations.add(deleteDoc);
113122

114-
115123
// begin bulkWriteNotOrderedExample
116124
BulkWriteOptions options = new BulkWriteOptions().ordered(false);
117125

126+
// Runs a bulk write operation for the specified insert, replace, update, and delete WriteModels in any order
118127
collection.bulkWrite(bulkOperations, options);
119128
//end bulkWriteNotOrderedExample
120129
}
121130

122131
private void bulkWriteExample() {
123132
// begin bulkWriteExample
124-
125133
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
126134

127-
135+
// Creates instructions to insert a document
128136
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
129-
.append("name", "Zaynab Omar")
130-
.append("age", 37));
137+
.append("name", "Zaynab Omar")
138+
.append("age", 37));
139+
140+
// Creates instructions to replace the first document matched by the query
131141
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"),
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"),
135147
Updates.set("name", "Zaynab Hassan"));
148+
149+
// Creates instructions to delete all documents matched by the query
136150
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50));
137151

138152
bulkOperations.add(insertDoc);
139153
bulkOperations.add(replaceDoc);
140154
bulkOperations.add(updateDoc);
141155
bulkOperations.add(deleteDoc);
142156

157+
// Runs a bulk write operation for the specified the insert, replace, update, and delete WriteModels in order
143158
collection.bulkWrite(bulkOperations);
144159
//end bulkWriteExample
145160
}
146161

147162
private void insertDocumentsExample(){
148163
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
149164

165+
// Creates instructions to insert multiple documents
150166
// begin insertDocumentsExample
151167
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie")
152168
.append("age", 17));
169+
153170
InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss")
154171
.append("age", 22));
155172
//end insertDocumentsExample
156173

157174
bulkOperations.add(juneDoc);
158175
bulkOperations.add(kevinDoc);
159176

177+
// Runs a bulk write operation for the specified insert WriteModels
160178
collection.bulkWrite(bulkOperations);
161179
}
162180

163181
private void replaceDocumentsExample(){
164182
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
165183

184+
// Creates instructions to replace the first document matched by the query
166185
// begin replaceDocumentsExample
167186
ReplaceOneModel<Document> celineDoc = new ReplaceOneModel<>(
168187
Filters.eq("_id", 1),
@@ -172,12 +191,14 @@ private void replaceDocumentsExample(){
172191

173192
bulkOperations.add(celineDoc);
174193

194+
// Runs a bulk write operation for the specified replace WriteModel
175195
collection.bulkWrite(bulkOperations);
176196
}
177197

178198
private void updateDocumentsExample(){
179199
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
180200

201+
// Creates instructions to update the first document matched by the query
181202
// begin updateDocumentsExample
182203
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(
183204
Filters.eq("_id", 2),
@@ -186,18 +207,21 @@ private void updateDocumentsExample(){
186207

187208
bulkOperations.add(updateDoc);
188209

210+
// Runs a bulk write operation for the specified update WriteModel
189211
collection.bulkWrite(bulkOperations);
190212
}
191213

192214
private void deleteDocumentsExample(){
193215
List<WriteModel<Document>> bulkOperations = new ArrayList<>();
194216

217+
// Creates instructions to delete the first document matched by the query
195218
// begin deleteDocumentsExample
196219
DeleteOneModel<Document> deleteDoc = new DeleteOneModel<>(Filters.eq("_id", 1));
197220
//end deleteDocumentsExample
198221

199222
bulkOperations.add(deleteDoc);
200223

224+
// Runs a bulk write operation for the specified delete WriteModel
201225
collection.bulkWrite(bulkOperations);
202226
}
203227

@@ -215,18 +239,19 @@ private void setUpCollection(){
215239
InsertOneModel<Document> karen = new InsertOneModel<>(new Document("_id", 1)
216240
.append("name", "Karen Sandoval")
217241
.append("age", 31));
242+
218243
InsertOneModel<Document> william = new InsertOneModel<>(new Document("_id", 2)
219244
.append("name", "William Chin")
220245
.append("age", 54));
246+
221247
InsertOneModel<Document> shayla = new InsertOneModel<>(new Document("_id", 8)
222248
.append("name", "Shayla Ray")
223249
.append("age", 20));
224-
250+
225251
bulkOperations.add(karen);
226252
bulkOperations.add(william);
227253
bulkOperations.add(shayla);
228254

229-
230255
collection.bulkWrite(bulkOperations);
231256
}
232257
}

0 commit comments

Comments
 (0)