Skip to content

Commit 6ca9e93

Browse files
authored
DOCSP-33345: Java comments pt. 3 (#471)
* DOCSP-33345: Java comments pt. 3 * CC edits * RR feedback + sheet syntax
1 parent 9399fbc commit 6ca9e93

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

source/includes/usage-examples/code-snippets/ReplaceOne.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Replaces the first document that matches a filter by using the Java driver
2+
13
package usage.examples;
24

35
import static com.mongodb.client.model.Filters.eq;
@@ -20,23 +22,27 @@ public static void main(String[] args) {
2022
String uri = "<connection string uri>";
2123

2224
try (MongoClient mongoClient = MongoClients.create(uri)) {
23-
2425
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2526
MongoCollection<Document> collection = database.getCollection("movies");
2627

2728
Bson query = eq("title", "Music of the Heart");
2829

30+
// Creates a new document containing "title" and "fullplot" fields
2931
Document replaceDocument = new Document().
3032
append("title", "50 Violins").
3133
append("fullplot", " A dramatization of the true story of Roberta Guaspari who co-founded the Opus 118 Harlem School of Music");
3234

35+
// Instructs the driver to insert a new document if none match the query
3336
ReplaceOptions opts = new ReplaceOptions().upsert(true);
3437

38+
// Replaces the first document that matches the filter with a new document
3539
UpdateResult result = collection.replaceOne(query, replaceDocument, opts);
3640

41+
// Prints the number of modified documents and the upserted document ID, if an upsert was performed
3742
System.out.println("Modified document count: " + result.getModifiedCount());
38-
System.out.println("Upserted id: " + result.getUpsertedId()); // only contains a value when an upsert is performed
43+
System.out.println("Upserted id: " + result.getUpsertedId());
3944

45+
// Prints a message if any exceptions occur during the operation
4046
} catch (MongoException me) {
4147
System.err.println("Unable to replace due to an error: " + me);
4248
}

source/includes/usage-examples/code-snippets/UpdateMany.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Updates documents that match a query filter by using the Java driver
2+
13
package usage.examples;
24

35
import static com.mongodb.client.model.Filters.gt;
@@ -20,21 +22,24 @@ public static void main(String[] args) {
2022
String uri = "<connection string uri>";
2123

2224
try (MongoClient mongoClient = MongoClients.create(uri)) {
23-
2425
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2526
MongoCollection<Document> collection = database.getCollection("movies");
2627

2728
Bson query = gt("num_mflix_comments", 50);
2829

30+
// Creates instructions to update the values of two document fields
2931
Bson updates = Updates.combine(
3032
Updates.addToSet("genres", "Frequently Discussed"),
3133
Updates.currentTimestamp("lastUpdated"));
3234

3335
try {
36+
// Updates documents that have a "num_mflix_comments" value over 50
3437
UpdateResult result = collection.updateMany(query, updates);
3538

39+
// Prints the number of updated documents
3640
System.out.println("Modified document count: " + result.getModifiedCount());
3741

42+
// Prints a message if any exceptions occur during the operation
3843
} catch (MongoException me) {
3944
System.err.println("Unable to update due to an error: " + me);
4045
}

source/includes/usage-examples/code-snippets/UpdateOne.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Updates the first document that matches a query filter by using the Java driver
2+
13
package usage.examples;
24

35
import org.bson.Document;
@@ -19,25 +21,29 @@ public static void main(String[] args) {
1921
String uri = "<connection string uri>";
2022

2123
try (MongoClient mongoClient = MongoClients.create(uri)) {
22-
2324
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2425
MongoCollection<Document> collection = database.getCollection("movies");
2526

2627
Document query = new Document().append("title", "Cool Runnings 2");
2728

29+
// Creates instructions to update the values of three document fields
2830
Bson updates = Updates.combine(
2931
Updates.set("runtime", 99),
3032
Updates.addToSet("genres", "Sports"),
3133
Updates.currentTimestamp("lastUpdated"));
3234

35+
// Instructs the driver to insert a new document if none match the query
3336
UpdateOptions options = new UpdateOptions().upsert(true);
3437

3538
try {
39+
// Updates the first document that has a "title" value of "Cool Runnings 2"
3640
UpdateResult result = collection.updateOne(query, updates, options);
3741

42+
// Prints the number of updated documents and the upserted document ID, if an upsert was performed
3843
System.out.println("Modified document count: " + result.getModifiedCount());
39-
40-
System.out.println("Upserted id: " + result.getUpsertedId()); // only contains a value when an upsert is performed
44+
System.out.println("Upserted id: " + result.getUpsertedId());
45+
46+
// Prints a message if any exceptions occur during the operation
4147
} catch (MongoException me) {
4248
System.err.println("Unable to update due to an error: " + me);
4349
}

source/includes/usage-examples/code-snippets/Watch.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
/**
2+
* This file demonstrates how to open a change stream by using the Java driver.
3+
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and listens
4+
* to change events in the "movies" collection. The code uses a change stream with a pipeline
5+
* to only filter for "insert" and "update" events.
6+
*/
7+
18
package usage.examples;
29

10+
import java.util.Arrays;
11+
import java.util.List;
12+
313
import org.bson.Document;
414
import org.bson.conversions.Bson;
515

@@ -18,18 +28,22 @@ public static void main( String[] args ) {
1828
String uri = "<connection string uri>";
1929

2030
try (MongoClient mongoClient = MongoClients.create(uri)) {
21-
2231
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2332
MongoCollection<Document> collection = database.getCollection("movies");
2433

34+
// Creates instructions to match insert and update operations
2535
List<Bson> pipeline = Arrays.asList(
2636
Aggregates.match(
2737
Filters.in("operationType",
2838
Arrays.asList("insert", "update"))));
39+
40+
// Creates a change stream that receives change events for the specified operations
2941
ChangeStreamIterable<Document> changeStream = database.watch(pipeline)
3042
.fullDocument(FullDocument.UPDATE_LOOKUP);
31-
// variables referenced in a lambda must be final; final array gives us a mutable integer
43+
3244
final int[] numberOfEvents = {0};
45+
46+
// Prints a message each time the change stream receives a change event, until it receives two events
3347
changeStream.forEach(event -> {
3448
System.out.println("Received a change to the collection: " + event);
3549
if (++numberOfEvents[0] >= 2) {

source/includes/usage-examples/code-snippets/WatchCompanion.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Performs CRUD operations to generate change events when run with the Watch application
2+
3+
14
package usage.examples;
25

36
import java.util.Arrays;
@@ -18,19 +21,23 @@ public static void main(String[] args) {
1821
String uri = "<connection string uri>";
1922

2023
try (MongoClient mongoClient = MongoClients.create(uri)) {
21-
2224
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2325
MongoCollection<Document> collection = database.getCollection("movies");
2426

2527
try {
28+
// Inserts a sample document into the "movies" collection and print its ID
2629
InsertOneResult insertResult = collection.insertOne(new Document("test", "sample movie document"));
2730
System.out.println("Success! Inserted document id: " + insertResult.getInsertedId());
2831

32+
// Updates the sample document and prints the number of modified documents
2933
UpdateResult updateResult = collection.updateOne(new Document("test", "sample movie document"), Updates.set("field2", "sample movie document update"));
3034
System.out.println("Updated " + updateResult.getModifiedCount() + " document.");
3135

36+
// Deletes the sample document and prints the number of deleted documents
3237
DeleteResult deleteResult = collection.deleteOne(new Document("field2", "sample movie document update"));
3338
System.out.println("Deleted " + deleteResult.getDeletedCount() + " document.");
39+
40+
// Prints a message if any exceptions occur during the operations
3441
} catch (MongoException me) {
3542
System.err.println("Unable to insert, update, or replace due to an error: " + me);
3643
}

0 commit comments

Comments
 (0)