Skip to content

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

Merged
merged 11 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion source/includes/fundamentals/code-snippets/AggTour.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bson.Document;

import java.util.Arrays;
import java.util.List;

// end imports

Expand All @@ -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")),
Expand All @@ -45,6 +48,7 @@ public static void main(String[] args) {
));
// end insert

// Groups matching documents by the "stars" field and accumulates a count of distinct "stars" values
// begin aggregation one
collection.aggregate(
Arrays.asList(
Expand All @@ -53,24 +57,28 @@ public static void main(String[] args) {
)
).forEach(doc -> System.out.println(doc.toJson()));
// end aggregation one

// Outputs the operation's winning and rejected execution plans
// 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 documents' "name" values and the values of a new "firstCategory" field
// begin aggregation two
collection.aggregate(
Arrays.asList(
Expand Down
50 changes: 37 additions & 13 deletions source/includes/fundamentals/code-snippets/BulkWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ private void insertExceptionExample() {
// begin insertExceptionExample
try {
List<WriteModel<Document>> bulkOperations = new ArrayList<>();

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 write operations on the insert WriteModels
collection.bulkWrite(bulkOperations);

// Prints a message if any exceptions occur during the operations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the code comments sheet:

Suggested change
// Prints a message if any exceptions occur during the operations
// Prints a message if any exceptions occur during the bulk write operation

} catch (MongoBulkWriteException e){
System.out.println("A MongoBulkWriteException occured with the following message: " + e.getMessage());
}
Expand All @@ -95,74 +96,90 @@ private void insertExceptionExample() {
private void bulkWriteNotOrderedExample() {
List<WriteModel<Document>> bulkOperations = new ArrayList<>();


// Creates instructions to insert a new document
InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6)
.append("name", "Zaynab Omar")
.append("age", 37));

// Creates instructions to replace the matching document
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the code comments sheet:

Suggested change
// Creates instructions to replace the matching document
// Creates instructions to replace the first document that matches the query

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 matching document
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"),
Updates.set("name", "Zaynab Hassan"));

// Creates instructions to delete the matching document
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 write operations on the 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 new document
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

For brevity, I think it could be good to omit the adjective "new" since it doesn't add any essential information and the term "new document" could be ambiguous.

Suggested change
// Creates instructions to insert a new document
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
// Creates instructions to replace the matching document
// Creates instructions to replace the matching document

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 the matching document
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue:
Since this is a deleteManyModel, it deletes all the matching documents.

Suggestion:

Suggested change
// Creates instructions to delete the matching document
// 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 write operations on 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 two new documents
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Creates instructions to insert two new documents
// 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 write operations on the 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),
Expand All @@ -172,12 +189,14 @@ private void replaceDocumentsExample(){

bulkOperations.add(celineDoc);

// Runs the write operation on the 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),
Expand All @@ -186,22 +205,26 @@ private void updateDocumentsExample(){

bulkOperations.add(updateDoc);

// Runs the write operation on the 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 the write operation on the delete WriteModel
collection.bulkWrite(bulkOperations);
}

private void preview(){
// Prints the JSON representation of each document in the collection
collection.find().forEach(doc -> System.out.println(doc.toJson()));
}

Expand All @@ -215,18 +238,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);
}
}
Loading