Skip to content

Commit caa94d2

Browse files
committed
DOCSP-33345: Java comments pt. 10 (#479)
(cherry picked from commit 23deccb)
1 parent 9d0698a commit caa94d2

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ public static void main(String[] args) {
2626
TimeSeriesOptions tsOptions = new TimeSeriesOptions("temperature");
2727
CreateCollectionOptions collOptions = new CreateCollectionOptions().timeSeriesOptions(tsOptions);
2828

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

3537
List<String> keys = Arrays.asList("cursor");
38+
39+
// Prints information about the database's collections and views
3640
System.out.println("listCollections: " + commandResult.getEmbedded(keys, Document.class).toJson());
3741
// end check collection type
42+
// Prints a message if any exceptions occur during the command execution
3843
} catch (MongoException me) {
3944
System.err.println("An error occurred: " + me);
4045
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,28 @@ public static void main(String [] args){
4242
}
4343

4444
private void updateManyExample(){
45+
// Creates a filter and update document to increase the "qty" value of all documents
4546
// begin updateManyExample
4647
Bson filter = Filters.empty();
4748
Bson update = Updates.inc("qty", 20);
49+
50+
// Updates all documents and prints the number of matched and modified documents
4851
UpdateResult result = collection.updateMany(filter, update);
4952
System.out.println("Matched document count: " + result.getMatchedCount());
5053
System.out.println("Modified document count: " + result.getModifiedCount());
5154
// end updateManyExample
5255
}
5356

5457
private void replaceOneExample(){
58+
// Creates a filter to match documents with a "color" value of "pink"
5559
// begin replaceOneExample
5660
Bson filter = Filters.eq("color", "pink");
5761
Document document = new Document("color", "orange").append("qty", 25);
62+
63+
// Replaces the first document that matches the filter with a new document
5864
UpdateResult result = collection.replaceOne(filter, document);
65+
66+
// Prints the number of matched and modified documents
5967
System.out.println("Matched document count: " + result.getMatchedCount());
6068
System.out.println("Modified document count: " + result.getModifiedCount());
6169
// end replaceOneExample

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,68 @@ public static void main(String [] args){
4949
}
5050

5151
private void updateValueExample(){
52+
// Creates a filter and update document to match a document and decrease its first "qty" array value
5253
// begin updateValueExample
5354
Bson filter = Filters.eq("qty", 18);
5455
Bson update = Updates.inc("qty.$", -3);
56+
57+
// Defines options that configure the operation to return a document in its post-operation state
5558
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
5659
.returnDocument(ReturnDocument.AFTER);
60+
61+
// Updates the first document that matches the filter and prints the updated document as JSON
5762
Document result = collection.findOneAndUpdate(filter, update, options);
5863
System.out.println(result.toJson());
5964
// end updateValueExample
6065
}
6166

6267
private void updateValueOptionsExample(){
68+
// Creates filter documents to match a document, then match the document's array values under 15
6369
// begin updateValueOptionsExample
6470
Bson filter = Filters.eq("_id", 1);
6571
Bson smallerFilter = Filters.lt("smaller", 15);
72+
73+
// Defines options that configure the document's return state and apply the array value filter
6674
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
6775
.returnDocument(ReturnDocument.AFTER)
6876
.arrayFilters(Arrays.asList(smallerFilter));
77+
78+
// Creates an update document to increase the matched array values by "5"
6979
Bson update = Updates.inc("qty.$[smaller]", 5);
7080

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

7687
private void updateAllElementsExample(){
88+
// Creates a filter and update document to match a document and double its "qty" array elements
7789
// begin updateAllElementsExample
7890
Bson filter = Filters.eq("_id", 1);
7991
Bson update = Updates.mul("qty.$[]", 2);
92+
93+
// Defines options that configure the operation to return a document in its post-operation state
8094
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
8195
.returnDocument(ReturnDocument.AFTER);
96+
97+
// Updates the first document that matches the filter and prints the updated document as JSON
8298
Document result = collection.findOneAndUpdate(filter, update, options);
8399
System.out.println(result.toJson());
84100
// end updateAllElementsExample
85101
}
86102

87103
private void pushElementsExample(){
104+
// Creates a filter and update document to match a document and add a value to its "qty" array
88105
// begin pushElementsExample
89106
Bson filter = Filters.eq("_id", 1);
90107
Bson update = Updates.push("qty", 17);
108+
109+
// Defines options that configure the operation to return a document in its post-operation state
91110
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
92111
.returnDocument(ReturnDocument.AFTER);
112+
113+
// Updates the first document that matches the filter and prints the updated document as JSON
93114
Document result = collection.findOneAndUpdate(filter, update, options);
94115
System.out.println(result.toJson());
95116
// end pushElementsExample

0 commit comments

Comments
 (0)