Skip to content

DOCSP-33345: Java comments pt. 10 #479

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 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions source/includes/fundamentals/code-snippets/TimeSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ public static void main(String[] args) {
TimeSeriesOptions tsOptions = new TimeSeriesOptions("temperature");
CreateCollectionOptions collOptions = new CreateCollectionOptions().timeSeriesOptions(tsOptions);

// Creates a time series collection that stores "temperature" values over time
database.createCollection("september2021", collOptions);
// end time series
try {
// Stores information about the database's collections and views in a document
// begin check collection type
Document commandResult = database.runCommand(new Document("listCollections", new BsonInt64(1)));

List<String> keys = Arrays.asList("cursor");

// Prints information about the database's collections and views
System.out.println("listCollections: " + commandResult.getEmbedded(keys, Document.class).toJson());
// end check collection type
// Prints a message if any exceptions occur during the command execution
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
Expand Down
8 changes: 8 additions & 0 deletions source/includes/fundamentals/code-snippets/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,28 @@ public static void main(String [] args){
}

private void updateManyExample(){
// Creates a filter and update document to increase the "qty" value of all documents
// begin updateManyExample
Bson filter = Filters.empty();
Bson update = Updates.inc("qty", 20);

// Updates all documents and prints the number of matched and modified documents
UpdateResult result = collection.updateMany(filter, update);
System.out.println("Matched document count: " + result.getMatchedCount());
System.out.println("Modified document count: " + result.getModifiedCount());
// end updateManyExample
}

private void replaceOneExample(){
// Creates a filter to match documents with a "color" value of "pink"
// begin replaceOneExample
Bson filter = Filters.eq("color", "pink");
Document document = new Document("color", "orange").append("qty", 25);

// Replaces the first document that matches the filter with a new document
UpdateResult result = collection.replaceOne(filter, document);

// Prints the number of matched and modified documents
System.out.println("Matched document count: " + result.getMatchedCount());
System.out.println("Modified document count: " + result.getModifiedCount());
// end replaceOneExample
Expand Down
21 changes: 21 additions & 0 deletions source/includes/fundamentals/code-snippets/UpdateArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,47 +49,68 @@ public static void main(String [] args){
}

private void updateValueExample(){
// Creates a filter and update document to match a document and decrease its first "qty" array value
// begin updateValueExample
Bson filter = Filters.eq("qty", 18);
Bson update = Updates.inc("qty.$", -3);

// Defines options that configure the operation to return a document in its post-operation state
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);

// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());
// end updateValueExample
}

private void updateValueOptionsExample(){
// Creates filter documents to match a document, then match the document's array values under 15
// begin updateValueOptionsExample
Bson filter = Filters.eq("_id", 1);
Bson smallerFilter = Filters.lt("smaller", 15);

// Defines options that configure the document's return state and apply the array value filter
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER)
.arrayFilters(Arrays.asList(smallerFilter));

// Creates an update document to increase the matched array values by "5"
Bson update = Updates.inc("qty.$[smaller]", 5);

// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());
// end updateValueOptionsExample
}

private void updateAllElementsExample(){
// Creates a filter and update document to match a document and double its "qty" array elements
// begin updateAllElementsExample
Bson filter = Filters.eq("_id", 1);
Bson update = Updates.mul("qty.$[]", 2);

// Defines options that configure the operation to return a document in its post-operation state
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);

// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());
// end updateAllElementsExample
}

private void pushElementsExample(){
// Creates a filter and update document to match a document and add a value to its "qty" array
// begin pushElementsExample
Bson filter = Filters.eq("_id", 1);
Bson update = Updates.push("qty", 17);

// Defines options that configure the operation to return a document in its post-operation state
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);

// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());
// end pushElementsExample
Expand Down