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 1 commit
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
12 changes: 11 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,21 @@ public static void main(String[] args) {
String uri = "<connection string uri>";

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

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

try {
// Find distinct "year" values for movies directed by "Carl Franklin"
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
MongoCursor<Integer> results = docs.iterator();

// Iterate through the distinct "year" values and print them
while(results.hasNext()) {
System.out.println(results.next());
}

// Handle any exceptions that might occur during the operation
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
Expand Down
13 changes: 12 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,21 @@ public static void main( String[] args ) {
String uri = "<connection string uri>";

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

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

// Define a projection to include the "title" and "imdb" fields and exclude "_id"
Bson projectionFields = Projections.fields(
Projections.include("title", "imdb"),
Projections.excludeId());

// Find documents that have a "runtime" value less than 15, apply the projection, and sort the results
MongoCursor<Document> cursor = collection.find(lt("runtime", 15))
.projection(projectionFields)
.sort(Sorts.descending("title")).iterator();

// Iterate through the returned documents and print them
try {
while(cursor.hasNext()) {
System.out.println(cursor.next().toJson());
Expand Down
12 changes: 11 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,22 @@ public static void main( String[] args ) {
String uri = "<connection string uri>";

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

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

// Define a projection to include the "title" and "imdb" fields and exclude "_id"
Bson projectionFields = Projections.fields(
Projections.include("title", "imdb"),
Projections.excludeId());

// Find documents with a "title" of "The Room", apply the projection, sort, and retrieve the first match
Document doc = collection.find(eq("title", "The Room"))
.projection(projectionFields)
.sort(Sorts.descending("imdb.rating"))
.first();

// Print the returned document, or print that none were found
if (doc == null) {
System.out.println("No results found.");
} else {
Expand Down
14 changes: 13 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,23 @@ public static void main(String[] args) {
String uri = "<connection string uri>";

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

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

// Define a list of documents to insert
List<Document> movieList = Arrays.asList(
new Document().append("title", "Short Circuit 3"),
new Document().append("title", "The Lego Frozen Movie"));

try {
// Insert the documents into the collection
InsertManyResult result = collection.insertMany(movieList);

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

// Handle any exceptions that might occur during the operation
} catch (MongoException me) {
System.err.println("Unable to insert due to an error: " + me);
}
Expand Down
13 changes: 12 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,21 @@ public static void main(String[] args) {
String uri = "<connection string uri>";

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

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

try {
// Create a document to insert
InsertOneResult result = collection.insertOne(new Document()
.append("_id", new ObjectId())
.append("title", "Ski Bloopers")
.append("genres", Arrays.asList("Documentary", "Comedy")));

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

// Handle any exceptions that might occur during the operation
} catch (MongoException me) {
System.err.println("Unable to insert due to an error: " + me);
}
Expand Down