Skip to content

Commit 6ed740d

Browse files
committed
remove try blocks
1 parent 924ba59 commit 6ed740d

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

source/includes/crud/BulkWrite.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,27 @@ public static void main(String[] args) {
2727
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2828
MongoCollection<Document> collection = database.getCollection("movies");
2929

30-
try {
31-
// Runs a bulk write operation for the specified insert, update, delete, and replace operations
32-
BulkWriteResult result = collection.bulkWrite(
33-
Arrays.asList(
34-
new InsertOneModel<>(new Document("name", "A Sample Movie")),
35-
new InsertOneModel<>(new Document("name", "Another Sample Movie")),
36-
new InsertOneModel<>(new Document("name", "Yet Another Sample Movie")),
37-
38-
new UpdateOneModel<>(new Document("name", "A Sample Movie"),
39-
new Document("$set", new Document("name", "An Old Sample Movie")),
40-
new UpdateOptions().upsert(true)),
41-
42-
new DeleteOneModel<>(new Document("name", "Yet Another Sample Movie")),
43-
44-
new ReplaceOneModel<>(new Document("name", "Yet Another Sample Movie"),
45-
new Document("name", "The Other Sample Movie").append("runtime", "42"))
46-
));
47-
// Prints the number of inserted, updated, and deleted documents
48-
System.out.println("Result statistics:" +
49-
"\ninserted: " + result.getInsertedCount() +
50-
"\nupdated: " + result.getModifiedCount() +
51-
"\ndeleted: " + result.getDeletedCount());
52-
53-
// Prints a message if any exceptions occur during the operations
54-
} catch (MongoException me) {
55-
System.err.println("The bulk write operation failed due to an error: " + me);
56-
}
30+
// Runs a bulk write operation for the specified insert, update, delete, and replace operations
31+
BulkWriteResult result = collection.bulkWrite(
32+
Arrays.asList(
33+
new InsertOneModel<>(new Document("name", "A Sample Movie")),
34+
new InsertOneModel<>(new Document("name", "Another Sample Movie")),
35+
new InsertOneModel<>(new Document("name", "Yet Another Sample Movie")),
36+
37+
new UpdateOneModel<>(new Document("name", "A Sample Movie"),
38+
new Document("$set", new Document("name", "An Old Sample Movie")),
39+
new UpdateOptions().upsert(true)),
40+
41+
new DeleteOneModel<>(new Document("name", "Yet Another Sample Movie")),
42+
43+
new ReplaceOneModel<>(new Document("name", "Yet Another Sample Movie"),
44+
new Document("name", "The Other Sample Movie").append("runtime", "42"))
45+
));
46+
// Prints the number of inserted, updated, and deleted documents
47+
System.out.println("Result statistics:" +
48+
"\ninserted: " + result.getInsertedCount() +
49+
"\nupdated: " + result.getModifiedCount() +
50+
"\ndeleted: " + result.getDeletedCount());
5751
}
5852
}
5953
}

source/includes/crud/Insert.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,25 @@ public static void main(String[] args) {
2626
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2727
MongoCollection<Document> collection = database.getCollection("movies");
2828

29-
try {
30-
// Inserts a sample document describing a movie into the collection
31-
InsertOneResult result = collection.insertOne(new Document()
32-
.append("_id", new ObjectId())
33-
.append("title", "Ski Bloopers")
34-
.append("genres", Arrays.asList("Documentary", "Comedy")));
35-
36-
// Prints the ID of the inserted document
37-
System.out.println("insertOne() document id: " + result.getInsertedId());
38-
39-
// Prints a message if any exceptions occur during the operation
40-
} catch (MongoException me) {
41-
System.err.println("Unable to insert due to an error: " + me);
42-
}
29+
// Inserts a sample document describing a movie into the collection
30+
InsertOneResult result = collection.insertOne(new Document()
31+
.append("_id", new ObjectId())
32+
.append("title", "Ski Bloopers")
33+
.append("genres", Arrays.asList("Documentary", "Comedy")));
34+
35+
// Prints the ID of the inserted document
36+
System.out.println("insertOne() document id: " + result.getInsertedId());
4337

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

49-
try {
50-
// Inserts sample documents describing movies into the collection
51-
InsertManyResult result = collection.insertMany(movieList);
43+
// Inserts sample documents describing movies into the collection
44+
InsertManyResult result = collection.insertMany(movieList);
5245

53-
// Prints the IDs of the inserted documents
54-
System.out.println("insertMany() document ids: " + result.getInsertedIds());
55-
} catch (MongoException me) {
56-
System.err.println("Unable to insert due to an error: " + me);
57-
}
46+
// Prints the IDs of the inserted documents
47+
System.out.println("insertMany() document ids: " + result.getInsertedIds());
5848
}
5949
}
6050
}

0 commit comments

Comments
 (0)