-
Notifications
You must be signed in to change notification settings - Fork 43
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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")) | ||
)); | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note as above.
Suggested change
|
||||||
* 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; | ||||||
|
@@ -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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* 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; | ||||||
|
@@ -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); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
} |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* 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. | ||||||
*/ | ||||||
|
||||||
norareidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
package usage.examples; | ||||||
|
||||||
import static com.mongodb.client.model.Filters.lt; | ||||||
|
@@ -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); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
} |
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. | ||||||||||||||
*/ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
package usage.examples; | ||||||||||||||
|
||||||||||||||
import static com.mongodb.client.model.Filters.eq; | ||||||||||||||
|
@@ -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); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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 //