Skip to content

DOCSP-33345: Java code comments pt. 2 #470

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 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion source/includes/usage-examples/code-snippets/Distinct.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* This file demonstrates how to find distinct values of a field by using the Java driver.
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds
* distinct "year" values for documents that match the specified query filter.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This file demonstrates how to find distinct values of a field by using the Java driver.
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds
* distinct "year" values for documents that match the specified query filter.
*/
// Find distinct values of a field by using the MongoDB Java driver


package usage.examples;

import org.bson.Document;
Expand All @@ -17,17 +23,20 @@ public static void main(String[] args) {
String uri = "<connection string uri>";

try (MongoClient mongoClient = MongoClients.create(uri)) {

MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");

try {
// Runs an operation that retrieves the distinct "year" values of matching documents
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
MongoCursor<Integer> results = docs.iterator();

// Prints the distinct "year" values
while(results.hasNext()) {
System.out.println(results.next());
}

// Prints a message if the operation generates an error
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
Expand Down
12 changes: 11 additions & 1 deletion source/includes/usage-examples/code-snippets/Find.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* This file demonstrates how to find multiple documents by using the Java driver.
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds
* documents in the "movies" collection that match the specified query filter.
* The code projects certain fields in the returned documents and sorts them according to
* specified criteria.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This file demonstrates how to find multiple documents by using the Java driver.
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds
* documents in the "movies" collection that match the specified query filter.
* The code projects certain fields in the returned documents and sorts them according to
* specified criteria.
*/
// Find multiple documents by using the MongoDB Java driver


package usage.examples;

import static com.mongodb.client.model.Filters.lt;
Expand All @@ -20,18 +28,20 @@ public static void main( String[] args ) {
String uri = "<connection string uri>";

try (MongoClient mongoClient = MongoClients.create(uri)) {

MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");

// Creates instructions to project two document fields
Bson projectionFields = Projections.fields(
Projections.include("title", "imdb"),
Projections.excludeId());

// Runs a find operation that matches documents with a "runtime" value under 15
MongoCursor<Document> cursor = collection.find(lt("runtime", 15))
.projection(projectionFields)
.sort(Sorts.descending("title")).iterator();

// Prints two fields of each matching document and sorts the result documents
try {
while(cursor.hasNext()) {
System.out.println(cursor.next().toJson());
Expand Down
11 changes: 10 additions & 1 deletion source/includes/usage-examples/code-snippets/FindOne.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This file demonstrates how to find a document by using the Java driver.
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds
* a document in the "movies" collection that matches the specified query filter.
* The code projects certain fields in the returned document.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This file demonstrates how to find a document by using the Java driver.
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds
* a document in the "movies" collection that matches the specified query filter.
* The code projects certain fields in the returned document.
*/
// Find a document by using the MongoDB Java driver


package usage.examples;

import static com.mongodb.client.model.Filters.eq;
Expand All @@ -20,19 +27,21 @@ public static void main( String[] args ) {
String uri = "<connection string uri>";

try (MongoClient mongoClient = MongoClients.create(uri)) {

MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");

// Creates instructions to project two document fields
Bson projectionFields = Projections.fields(
Projections.include("title", "imdb"),
Projections.excludeId());

// Runs a find operation that retrieves only the first matching document
Document doc = collection.find(eq("title", "The Room"))
.projection(projectionFields)
.sort(Sorts.descending("imdb.rating"))
.first();

// Prints a message if there are no result documents, or prints two fields of the matched document
if (doc == null) {
System.out.println("No results found.");
} else {
Expand Down
13 changes: 12 additions & 1 deletion source/includes/usage-examples/code-snippets/InsertMany.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This file demonstrates how to insert multiple documents into a collection by
* using the Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and inserts two new documents into the "movies" collection.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This file demonstrates how to insert multiple documents into a collection by
* using the Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and inserts two new documents into the "movies" collection.
*/
// Insert multiple documents into a collection using the Java driver.


package usage.examples;

import java.util.Arrays;
Expand All @@ -19,18 +26,22 @@ public static void main(String[] args) {
String uri = "<connection string uri>";

try (MongoClient mongoClient = MongoClients.create(uri)) {

MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");

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

try {
// Runs a write operation that inserts sample documents into the collection
InsertManyResult result = collection.insertMany(movieList);

// Prints the IDs of the inserted documents
System.out.println("Inserted document ids: " + result.getInsertedIds());

// Prints a message if the operation generates an error
} catch (MongoException me) {
System.err.println("Unable to insert due to an error: " + me);
}
Expand Down
12 changes: 11 additions & 1 deletion source/includes/usage-examples/code-snippets/InsertOne.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This file demonstrates how to insert a document into a collection by using the
* Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and inserts a new document into the "movies" collection.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This file demonstrates how to insert a document into a collection by using the
* Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and inserts a new document into the "movies" collection.
*/
// Insert a document into a collection using the MongoDB Java driver.


package usage.examples;

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

try (MongoClient mongoClient = MongoClients.create(uri)) {

MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");

try {
// Runs a write operation that inserts a sample document into the collection
InsertOneResult result = collection.insertOne(new Document()
.append("_id", new ObjectId())
.append("title", "Ski Bloopers")
.append("genres", Arrays.asList("Documentary", "Comedy")));

// Prints the ID of the inserted document
System.out.println("Success! Inserted document id: " + result.getInsertedId());

// Prints a message if the operation generates an error
} catch (MongoException me) {
System.err.println("Unable to insert due to an error: " + me);
}
Expand Down