Skip to content

Commit 344bca7

Browse files
committed
DOCSP-33345: Java code comments pt. 1 (#469)
* DOCSP-33345: Java code comments pt. 1 * file summaries * indicative mood * CC edits * CD feedback + sheet syntax (cherry picked from commit d198b0f)
1 parent dd4be30 commit 344bca7

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Runs bulk write operations on a collection by using the Java driver
2+
13
package usage.examples;
24

35
import java.util.Arrays;
@@ -22,33 +24,36 @@ public static void main(String[] args) {
2224
String uri = "<connection string uri>";
2325

2426
try (MongoClient mongoClient = MongoClients.create(uri)) {
25-
2627
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2728
MongoCollection<Document> collection = database.getCollection("movies");
2829

2930
try {
31+
// Runs a bulk write operation for the specified insert, update, delete, and replace operations
3032
BulkWriteResult result = collection.bulkWrite(
3133
Arrays.asList(
3234
new InsertOneModel<>(new Document("name", "A Sample Movie")),
3335
new InsertOneModel<>(new Document("name", "Another Sample Movie")),
3436
new InsertOneModel<>(new Document("name", "Yet Another Sample Movie")),
37+
3538
new UpdateOneModel<>(new Document("name", "A Sample Movie"),
3639
new Document("$set", new Document("name", "An Old Sample Movie")),
3740
new UpdateOptions().upsert(true)),
41+
3842
new DeleteOneModel<>(new Document("name", "Yet Another Sample Movie")),
43+
3944
new ReplaceOneModel<>(new Document("name", "Yet Another Sample Movie"),
4045
new Document("name", "The Other Sample Movie").append("runtime", "42"))
4146
));
42-
47+
// Prints the number of inserted, updated, and deleted documents
4348
System.out.println("Result statistics:" +
4449
"\ninserted: " + result.getInsertedCount() +
4550
"\nupdated: " + result.getModifiedCount() +
4651
"\ndeleted: " + result.getDeletedCount());
4752

53+
// Prints a message if any exceptions occur during the operations
4854
} catch (MongoException me) {
4955
System.err.println("The bulk write operation failed due to an error: " + me);
5056
}
5157
}
5258
}
53-
}
54-
59+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Runs a database command by using the Java driver
2+
13
package usage.examples;
24

35
import org.bson.BsonDocument;
@@ -23,11 +25,17 @@ public static void main(String[] args) {
2325

2426
try {
2527
Bson command = new BsonDocument("dbStats", new BsonInt64(1));
28+
29+
// Retrieves statistics about the specified database
2630
Document commandResult = database.runCommand(command);
31+
32+
// Prints the database statistics
2733
System.out.println("dbStats: " + commandResult.toJson());
34+
35+
// Prints a message if any exceptions occur during the command execution
2836
} catch (MongoException me) {
2937
System.err.println("An error occurred: " + me);
3038
}
3139
}
3240
}
33-
}
41+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Runs count operations on a collection by using the Java driver
2+
13
package usage.examples;
24

35
import static com.mongodb.client.model.Filters.eq;
@@ -17,22 +19,24 @@ public static void main(String[] args) {
1719
String uri = "<connection string uri>";
1820

1921
try (MongoClient mongoClient = MongoClients.create(uri)) {
20-
2122
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2223
MongoCollection<Document> collection = database.getCollection("movies");
2324

2425
Bson query = eq("countries", "Spain");
2526

2627
try {
28+
// Retrieves and prints the estimated number of documents in the collection
2729
long estimatedCount = collection.estimatedDocumentCount();
2830
System.out.println("Estimated number of documents in the movies collection: " + estimatedCount);
2931

32+
// Retrieves and prints the number of documents with a "countries" value of "Spain"
3033
long matchingCount = collection.countDocuments(query);
3134
System.out.println("Number of movies from Spain: " + matchingCount);
35+
36+
// Prints a message if any exceptions occur during the operations
3237
} catch (MongoException me) {
3338
System.err.println("An error occurred: " + me);
3439
}
3540
}
3641
}
37-
}
38-
42+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Deletes multiple documents from a collection by using the Java driver
2+
13
package usage.examples;
24

35
import static com.mongodb.client.model.Filters.lt;
@@ -25,12 +27,16 @@ public static void main(String[] args) {
2527
Bson query = lt("imdb.rating", 1.9);
2628

2729
try {
30+
// Deletes all documents that have an "imdb.rating" value less than 1.9
2831
DeleteResult result = collection.deleteMany(query);
32+
33+
// Prints the number of deleted documents
2934
System.out.println("Deleted document count: " + result.getDeletedCount());
35+
36+
// Prints a message if any exceptions occur during the operation
3037
} catch (MongoException me) {
3138
System.err.println("Unable to delete due to an error: " + me);
3239
}
3340
}
3441
}
35-
}
36-
42+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Deletes a document from a collection by using the Java driver
2+
13
package usage.examples;
24

35
import static com.mongodb.client.model.Filters.eq;
@@ -26,12 +28,14 @@ public static void main(String[] args) {
2628
Bson query = eq("title", "The Garbage Pail Kids Movie");
2729

2830
try {
31+
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
2932
DeleteResult result = collection.deleteOne(query);
3033
System.out.println("Deleted document count: " + result.getDeletedCount());
34+
35+
// Prints a message if any exceptions occur during the operation
3136
} catch (MongoException me) {
3237
System.err.println("Unable to delete due to an error: " + me);
3338
}
3439
}
3540
}
36-
}
37-
41+
}

0 commit comments

Comments
 (0)