Skip to content

DOCSP-33345: Java code comments pt. 1 #469

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 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 12 additions & 3 deletions source/includes/usage-examples/code-snippets/BulkWrite.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* This file demonstrates MongoDB bulk write operations by using the Java driver.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Personal opinion: There's something about the "This file" that feels clunky to me. I think maybe we delete that and change to:

Suggested change
* This file demonstrates MongoDB bulk write operations by using the Java driver.
* Conduct bulk write operations by using the Java driver.

Copy link
Collaborator

Choose a reason for hiding this comment

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

And change comment to single line comment //

* It connects to a MongoDB deployment, accesses the "sample_mflix" database,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think delete the next two rows.

* and performs bulk write operations on the "movies" collection.
*/

package usage.examples;

import java.util.Arrays;
Expand All @@ -22,20 +28,23 @@ 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 write operations on insert, update, delete, and replace WriteModels
BulkWriteResult result = collection.bulkWrite(
Arrays.asList(
new InsertOneModel<>(new Document("name", "A Sample Movie")),
new InsertOneModel<>(new Document("name", "Another Sample Movie")),
new InsertOneModel<>(new Document("name", "Yet Another Sample Movie")),

new UpdateOneModel<>(new Document("name", "A Sample Movie"),
new Document("$set", new Document("name", "An Old Sample Movie")),
new UpdateOptions().upsert(true)),

new DeleteOneModel<>(new Document("name", "Yet Another Sample Movie")),

new ReplaceOneModel<>(new Document("name", "Yet Another Sample Movie"),
new Document("name", "The Other Sample Movie").append("runtime", "42"))
));
Expand All @@ -45,10 +54,10 @@ public static void main(String[] args) {
"\nupdated: " + result.getModifiedCount() +
"\ndeleted: " + result.getDeletedCount());

// Prints a message if an error occurs during the operations
} catch (MongoException me) {
System.err.println("The bulk write operation failed due to an error: " + me);
}
}
}
}

}
13 changes: 12 additions & 1 deletion source/includes/usage-examples/code-snippets/Command.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* This file demonstrates how to run a command by using the Java driver.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same note as above.

Suggested change
* This file demonstrates how to run a command by using the Java driver.
* // Run a command by using the MongoDB Java driver.

* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and runs a command to retrieve database statistics.
*/

package usage.examples;

import org.bson.BsonDocument;
Expand All @@ -22,12 +28,17 @@ public static void main(String[] args) {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");

try {
// Defines and runs a command to retrieve database statistics
Bson command = new BsonDocument("dbStats", new BsonInt64(1));
Document commandResult = database.runCommand(command);

// Prints the database statistics
System.out.println("dbStats: " + commandResult.toJson());

// Prints a message if an error occurs during the operation
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This file demonstrates how to perform count operations by using the Java driver.
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 perform count operations by using the Java driver.
* // Perform count operations by using the MongoDB Java driver.

* It connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and counts the number of documents in the "movies" collection.
* The code also counts the number of documents that match a specified query.
*/

package usage.examples;

import static com.mongodb.client.model.Filters.eq;
Expand All @@ -17,22 +24,24 @@ 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");

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

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

// Counts and prints the number of documents with a "countries" value of "Spain"
long matchingCount = collection.countDocuments(query);
System.out.println("Number of movies from Spain: " + matchingCount);

// Prints a message if an error occurs during the operation
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
}
}
}

}
15 changes: 13 additions & 2 deletions source/includes/usage-examples/code-snippets/DeleteMany.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This file demonstrates how to delete multiple documents in a collection by
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 delete multiple documents in a collection by
* // Delete multiple documents in a collection using the MongoDB Java driver.

* using the Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and deletes documents in the "movies" collection based on a specified query.
*/

package usage.examples;

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

try {
// Runs an operation to delete documents that have an "imdb.rating" value less than 1.9
DeleteResult result = collection.deleteMany(query);

// Prints the number of deleted documents
System.out.println("Deleted document count: " + result.getDeletedCount());

// Prints a message if an error occurs during the operation
} catch (MongoException me) {
System.err.println("Unable to delete due to an error: " + me);
}
}
}
}

}
13 changes: 11 additions & 2 deletions source/includes/usage-examples/code-snippets/DeleteOne.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This file demonstrates how to delete a document in a collection by using the
* Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and deletes a document in the "movies" collection based on a specified query.
*/
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 delete a document in a collection by using the
* Java driver.
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database,
* and deletes a document in the "movies" collection based on a specified query.
*/
* // Delete a document in a collection by using the MongoDB Java driver.


package usage.examples;

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

try {
// Runs an operation to delete the document with a "title" of "The Garbage Pail Kids Movie"
DeleteResult result = collection.deleteOne(query);
System.out.println("Deleted document count: " + result.getDeletedCount());

// Prints a message if an error occurs during the operation
} catch (MongoException me) {
System.err.println("Unable to delete due to an error: " + me);
}
}
}
}

}