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 all 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
13 changes: 9 additions & 4 deletions source/includes/usage-examples/code-snippets/BulkWrite.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Runs bulk write operations on a collection by using the Java driver

package usage.examples;

import java.util.Arrays;
Expand All @@ -22,33 +24,36 @@ 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 bulk write operation for the specified insert, update, delete, and replace operations
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"))
));

// Prints the number of inserted, updated, and deleted documents
System.out.println("Result statistics:" +
"\ninserted: " + result.getInsertedCount() +
"\nupdated: " + result.getModifiedCount() +
"\ndeleted: " + result.getDeletedCount());

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

}
10 changes: 9 additions & 1 deletion source/includes/usage-examples/code-snippets/Command.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Runs a database command by using the Java driver

package usage.examples;

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

try {
Bson command = new BsonDocument("dbStats", new BsonInt64(1));

// Retrieves statistics about the specified database
Document commandResult = database.runCommand(command);

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

// Prints a message if any exceptions occur during the command execution
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Runs count operations on a collection by using the Java driver

package usage.examples;

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

// Retrieves 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 any exceptions occur during the operations
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
}
}
}

}
10 changes: 8 additions & 2 deletions source/includes/usage-examples/code-snippets/DeleteMany.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Deletes multiple documents from a collection by using the Java driver

package usage.examples;

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

try {
// Deletes all 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 any exceptions occur during the operation
} catch (MongoException me) {
System.err.println("Unable to delete due to an error: " + me);
}
}
}
}

}
8 changes: 6 additions & 2 deletions source/includes/usage-examples/code-snippets/DeleteOne.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Deletes a document from a collection by using the Java driver

package usage.examples;

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

try {
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
DeleteResult result = collection.deleteOne(query);
System.out.println("Deleted document count: " + result.getDeletedCount());

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

}