Skip to content

Commit 5b63fb4

Browse files
committed
DOCSP-33345: Java code comments pt. 2 (#470)
* DOCSP-33345: Java code comments pt. 2 * CC edits * CD feedback + sheet syntax (cherry picked from commit 9399fbc)
1 parent 69ad964 commit 5b63fb4

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Retrieves distinct values of a field by using the Java driver
2+
13
package usage.examples;
24

35
import org.bson.Document;
@@ -17,17 +19,20 @@ 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
try {
26+
// Retrieves the distinct values of the "year" field present in documents that match the filter
2527
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
2628
MongoCursor<Integer> results = docs.iterator();
2729

30+
// Prints the distinct "year" values
2831
while(results.hasNext()) {
2932
System.out.println(results.next());
3033
}
34+
35+
// Prints a message if any exceptions occur during the operation
3136
} catch (MongoException me) {
3237
System.err.println("An error occurred: " + me);
3338
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Retrieves 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.lt;
@@ -20,18 +22,20 @@ 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

28+
// Creates instructions to project two document fields
2729
Bson projectionFields = Projections.fields(
2830
Projections.include("title", "imdb"),
2931
Projections.excludeId());
3032

33+
// Retrieves documents that match the filter, applying a projection and a descending sort to the results
3134
MongoCursor<Document> cursor = collection.find(lt("runtime", 15))
3235
.projection(projectionFields)
3336
.sort(Sorts.descending("title")).iterator();
3437

38+
// Prints the results of the find operation as JSON
3539
try {
3640
while(cursor.hasNext()) {
3741
System.out.println(cursor.next().toJson());

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

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

35
import static com.mongodb.client.model.Filters.eq;
@@ -20,19 +22,21 @@ 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

28+
// Creates instructions to project two document fields
2729
Bson projectionFields = Projections.fields(
2830
Projections.include("title", "imdb"),
2931
Projections.excludeId());
3032

33+
// Retrieves the first matching document, applying a projection and a descending sort to the results
3134
Document doc = collection.find(eq("title", "The Room"))
3235
.projection(projectionFields)
3336
.sort(Sorts.descending("imdb.rating"))
3437
.first();
3538

39+
// Prints a message if there are no result documents, or prints the result document as JSON
3640
if (doc == null) {
3741
System.out.println("No results found.");
3842
} else {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Inserts sample documents describing movies by using the Java driver
2+
13
package usage.examples;
24

35
import java.util.Arrays;
@@ -19,18 +21,22 @@ 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

27+
// Creates two sample documents containing a "title" field
2628
List<Document> movieList = Arrays.asList(
2729
new Document().append("title", "Short Circuit 3"),
2830
new Document().append("title", "The Lego Frozen Movie"));
2931

3032
try {
33+
// Inserts sample documents describing movies into the collection
3134
InsertManyResult result = collection.insertMany(movieList);
3235

36+
// Prints the IDs of the inserted documents
3337
System.out.println("Inserted document ids: " + result.getInsertedIds());
38+
39+
// Prints a message if any exceptions occur during the operation
3440
} catch (MongoException me) {
3541
System.err.println("Unable to insert due to an error: " + me);
3642
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Inserts a sample document describing a movie by using the Java driver
2+
13
package usage.examples;
24

35
import java.util.Arrays;
@@ -18,17 +20,20 @@ public static void main(String[] args) {
1820
String uri = "<connection string uri>";
1921

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

2526
try {
27+
// Inserts a sample document describing a movie into the collection
2628
InsertOneResult result = collection.insertOne(new Document()
2729
.append("_id", new ObjectId())
2830
.append("title", "Ski Bloopers")
2931
.append("genres", Arrays.asList("Documentary", "Comedy")));
3032

33+
// Prints the ID of the inserted document
3134
System.out.println("Success! Inserted document id: " + result.getInsertedId());
35+
36+
// Prints a message if any exceptions occur during the operation
3237
} catch (MongoException me) {
3338
System.err.println("Unable to insert due to an error: " + me);
3439
}

0 commit comments

Comments
 (0)