-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-33345: Java comments pt. 4 #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
ce57a6d
e9bc6df
b001ba6
b6290a7
a748ddf
3630611
2ae0dc9
e6c270b
ec30862
68f46ec
55966ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import org.bson.Document; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
// end imports | ||
|
||
|
@@ -30,6 +31,8 @@ public static void main(String[] args) { | |
// end connection | ||
|
||
collection.drop(); | ||
|
||
// Inserts sample documents describing restaurants | ||
// begin insert | ||
collection.insertMany(Arrays.asList( | ||
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) { | |
)); | ||
// end insert | ||
|
||
// Creates an aggregation pipeline that matches documents, groups them by the "stars" field, and tallies them by distinct values | ||
// begin aggregation one | ||
collection.aggregate( | ||
Arrays.asList( | ||
Aggregates.match(Filters.eq("categories", "Bakery")), | ||
Aggregates.group("$stars", Accumulators.sum("count", 1)) | ||
) | ||
// Prints the result of the aggregation operation as JSON | ||
).forEach(doc -> System.out.println(doc.toJson())); | ||
// end aggregation one | ||
|
||
// begin aggregation three | ||
Document explanation = collection.aggregate( | ||
Arrays.asList( | ||
Aggregates.match(Filters.eq("categories", "bakery")), | ||
Aggregates.match(Filters.eq("categories", "Bakery")), | ||
Aggregates.group("$stars", Accumulators.sum("count", 1)) | ||
) | ||
).explain(ExplainVerbosity.EXECUTION_STATS); | ||
|
||
List<Document> stages = explanation.get("stages", List.class); | ||
List<String> keys = Arrays.asList("queryPlanner", "winningPlan"); | ||
|
||
// Prints the JSON representation of the winning execution plans | ||
for (Document stage : stages) { | ||
Document cursorStage = stage.get("$cursor", Document.class); | ||
if (cursorStage != null) { | ||
System.out.println(cursorStage.getEmbedded(keys, Document.class).toJson()); | ||
} | ||
} | ||
// end aggregation three | ||
// Prints the restaurant name and the first value in the "categories" array as a field named "firstCategory" | ||
// begin aggregation two | ||
collection.aggregate( | ||
Arrays.asList( | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -77,92 +77,111 @@ private void insertExceptionExample() { | |||||
// begin insertExceptionExample | ||||||
try { | ||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
|
||||||
// Creates instructions to insert documents | ||||||
InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1)); | ||||||
InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 3)); | ||||||
|
||||||
bulkOperations.add(doc1); | ||||||
bulkOperations.add(doc3); | ||||||
|
||||||
// Runs a bulk write operation for the specified insert WriteModels | ||||||
collection.bulkWrite(bulkOperations); | ||||||
|
||||||
// Prints a message if any exceptions occur during the operations | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the code comments sheet:
Suggested change
|
||||||
} catch (MongoBulkWriteException e){ | ||||||
System.out.println("A MongoBulkWriteException occured with the following message: " + e.getMessage()); | ||||||
System.out.println("A MongoBulkWriteException occurred with the following message: " + e.getMessage()); | ||||||
} | ||||||
//end insertExceptionExample | ||||||
} | ||||||
|
||||||
private void bulkWriteNotOrderedExample() { | ||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
|
||||||
// Creates instructions to insert a document | ||||||
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6) | ||||||
.append("name", "Zaynab Omar") | ||||||
.append("age", 37)); | ||||||
|
||||||
// Creates instructions to replace the matching document | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the code comments sheet:
Suggested change
|
||||||
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1), | ||||||
new Document("name", "Sandy Kane") | ||||||
.append("location", "Helena, MT")); | ||||||
.append("location", "Helena, MT")); | ||||||
|
||||||
// Creates instructions to update the first document that matches the query | ||||||
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"), | ||||||
Updates.set("name", "Zaynab Hassan")); | ||||||
|
||||||
// Creates instructions to delete all matching documents | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: I think it would be good for consistency to mention matching the query. Added to the code comment sheet.
Suggested change
|
||||||
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50)); | ||||||
|
||||||
bulkOperations.add(insertDoc); | ||||||
bulkOperations.add(replaceDoc); | ||||||
bulkOperations.add(updateDoc); | ||||||
bulkOperations.add(deleteDoc); | ||||||
|
||||||
|
||||||
// begin bulkWriteNotOrderedExample | ||||||
BulkWriteOptions options = new BulkWriteOptions().ordered(false); | ||||||
|
||||||
// Runs a bulk write operation for the specified insert, replace, update, and delete WriteModels in any order | ||||||
collection.bulkWrite(bulkOperations, options); | ||||||
//end bulkWriteNotOrderedExample | ||||||
} | ||||||
|
||||||
private void bulkWriteExample() { | ||||||
// begin bulkWriteExample | ||||||
|
||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
|
||||||
// Creates instructions to insert a document | ||||||
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6) | ||||||
.append("name", "Zaynab Omar") | ||||||
.append("age", 37)); | ||||||
.append("name", "Zaynab Omar") | ||||||
.append("age", 37)); | ||||||
|
||||||
// Creates instructions to replace the matching document | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: I think it's important to mention the first document matched by the query for all instances of "OneModel"s as described on the code comments sheet as it is otherwise inaccurate for the case in which there are more than one matches:
Suggested change
Applies to all instances. |
||||||
ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1), | ||||||
new Document("name", "Sandy Kane") | ||||||
.append("location", "Helena, MT")); | ||||||
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"), | ||||||
new Document("name", "Sandy Kane") | ||||||
.append("location", "Helena, MT")); | ||||||
|
||||||
// Creates instructions to update the matching document | ||||||
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"), | ||||||
Updates.set("name", "Zaynab Hassan")); | ||||||
|
||||||
// Creates instructions to delete all matching documents | ||||||
DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50)); | ||||||
|
||||||
bulkOperations.add(insertDoc); | ||||||
bulkOperations.add(replaceDoc); | ||||||
bulkOperations.add(updateDoc); | ||||||
bulkOperations.add(deleteDoc); | ||||||
|
||||||
// Runs a bulk write operation for the specified the insert, replace, update, and delete WriteModels in order | ||||||
collection.bulkWrite(bulkOperations); | ||||||
//end bulkWriteExample | ||||||
} | ||||||
|
||||||
private void insertDocumentsExample(){ | ||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
// Creates instructions to insert multiple documents | ||||||
// begin insertDocumentsExample | ||||||
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie") | ||||||
.append("age", 17)); | ||||||
|
||||||
InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss") | ||||||
.append("age", 22)); | ||||||
//end insertDocumentsExample | ||||||
|
||||||
bulkOperations.add(juneDoc); | ||||||
bulkOperations.add(kevinDoc); | ||||||
|
||||||
// Runs a bulk write operation for the specified insert WriteModels | ||||||
collection.bulkWrite(bulkOperations); | ||||||
} | ||||||
|
||||||
private void replaceDocumentsExample(){ | ||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
// Creates instructions to replace the matching document | ||||||
// begin replaceDocumentsExample | ||||||
ReplaceOneModel<Document> celineDoc = new ReplaceOneModel<>( | ||||||
Filters.eq("_id", 1), | ||||||
|
@@ -172,12 +191,14 @@ private void replaceDocumentsExample(){ | |||||
|
||||||
bulkOperations.add(celineDoc); | ||||||
|
||||||
// Runs a bulk write operation for the specified replace WriteModel | ||||||
collection.bulkWrite(bulkOperations); | ||||||
} | ||||||
|
||||||
private void updateDocumentsExample(){ | ||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
// Creates instructions to update the matching document | ||||||
// begin updateDocumentsExample | ||||||
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>( | ||||||
Filters.eq("_id", 2), | ||||||
|
@@ -186,18 +207,21 @@ private void updateDocumentsExample(){ | |||||
|
||||||
bulkOperations.add(updateDoc); | ||||||
|
||||||
// Runs a bulk write operation for the specified update WriteModel | ||||||
collection.bulkWrite(bulkOperations); | ||||||
} | ||||||
|
||||||
private void deleteDocumentsExample(){ | ||||||
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); | ||||||
|
||||||
// Creates instructions to delete the matching document | ||||||
// begin deleteDocumentsExample | ||||||
DeleteOneModel<Document> deleteDoc = new DeleteOneModel<>(Filters.eq("_id", 1)); | ||||||
//end deleteDocumentsExample | ||||||
|
||||||
bulkOperations.add(deleteDoc); | ||||||
|
||||||
// Runs a bulk write operation for the specified delete WriteModel | ||||||
collection.bulkWrite(bulkOperations); | ||||||
} | ||||||
|
||||||
|
@@ -215,18 +239,19 @@ private void setUpCollection(){ | |||||
InsertOneModel<Document> karen = new InsertOneModel<>(new Document("_id", 1) | ||||||
.append("name", "Karen Sandoval") | ||||||
.append("age", 31)); | ||||||
|
||||||
InsertOneModel<Document> william = new InsertOneModel<>(new Document("_id", 2) | ||||||
.append("name", "William Chin") | ||||||
.append("age", 54)); | ||||||
|
||||||
InsertOneModel<Document> shayla = new InsertOneModel<>(new Document("_id", 8) | ||||||
.append("name", "Shayla Ray") | ||||||
.append("age", 20)); | ||||||
|
||||||
bulkOperations.add(karen); | ||||||
bulkOperations.add(william); | ||||||
bulkOperations.add(shayla); | ||||||
|
||||||
|
||||||
collection.bulkWrite(bulkOperations); | ||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.