Skip to content

Commit c388925

Browse files
committed
Java Update ready for PR
1 parent d71a182 commit c388925

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<maven-compiler-plugin.source>8</maven-compiler-plugin.source>
1313
<maven-compiler-plugin.target>8</maven-compiler-plugin.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<mongodb-driver-sync.version>3.11.0</mongodb-driver-sync.version>
15+
<mongodb-driver-sync.version>3.11.2</mongodb-driver-sync.version>
1616
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
1717
</properties>
1818

src/main/java/com/mongodb/quickstart/Update.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,72 @@
44
import com.mongodb.client.MongoClients;
55
import com.mongodb.client.MongoCollection;
66
import com.mongodb.client.MongoDatabase;
7-
import com.mongodb.client.model.*;
7+
import com.mongodb.client.model.FindOneAndUpdateOptions;
8+
import com.mongodb.client.model.ReturnDocument;
9+
import com.mongodb.client.model.UpdateOptions;
810
import com.mongodb.client.result.UpdateResult;
911
import org.bson.Document;
1012
import org.bson.conversions.Bson;
1113
import org.bson.json.JsonWriterSettings;
1214

13-
import java.util.Arrays;
14-
import java.util.List;
1515
import java.util.logging.Level;
1616
import java.util.logging.Logger;
1717

18+
import static com.mongodb.client.model.Filters.and;
19+
import static com.mongodb.client.model.Filters.eq;
20+
import static com.mongodb.client.model.Updates.*;
21+
1822
public class Update {
1923

2024
public static void main(String[] args) {
21-
Logger.getLogger("org.mongodb.driver").setLevel(Level.WARNING);
25+
Logger.getLogger("org.mongodb.driver").setLevel(Level.ALL);
2226
JsonWriterSettings prettyPrint = JsonWriterSettings.builder().indent(true).build();
2327

2428
try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
2529
MongoDatabase sampleTrainingDB = mongoClient.getDatabase("sample_training");
2630
MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("grades");
2731

2832
// update one document
29-
Bson filter = Filters.eq("student_id", 10000);
30-
Bson updateOperation = Updates.set("comment", "You should learn MongoDB!");
33+
Bson filter = eq("student_id", 10000);
34+
Bson updateOperation = set("comment", "You should learn MongoDB!");
3135
UpdateResult updateResult = gradesCollection.updateOne(filter, updateOperation);
32-
System.out.println("\nUpdating the doc with {\"student_id\":10000}. Adding comment.");
36+
System.out.println("=> Updating the doc with {\"student_id\":10000}. Adding comment.");
3337
System.out.println(gradesCollection.find(filter).first().toJson(prettyPrint));
3438
System.out.println(updateResult);
3539

3640
// upsert
37-
filter = Filters.eq("student_id", 10002);
38-
updateOperation = Updates.push("comments", "You will learn a lot if you read the MongoDB blog!");
41+
filter = and(eq("student_id", 10002d), eq("class_id", 10d));
42+
updateOperation = push("comments", "You will learn a lot if you read the MongoDB blog!");
3943
UpdateOptions options = new UpdateOptions().upsert(true);
4044
updateResult = gradesCollection.updateOne(filter, updateOperation, options);
41-
System.out.println("\nUpsert document with {\"student_id\":10002} because it doesn't exist yet.");
45+
System.out.println("\n=> Upsert document with {\"student_id\":10002.0, \"class_id\": 10.0} because it doesn't exist yet.");
4246
System.out.println(updateResult);
4347
System.out.println(gradesCollection.find(filter).first().toJson(prettyPrint));
4448

4549
// update many documents
46-
filter = Filters.eq("student_id", 10001);
50+
filter = eq("student_id", 10001);
4751
updateResult = gradesCollection.updateMany(filter, updateOperation);
48-
System.out.println("\nUpdating all the documents with {\"student_id\":10001}.");
52+
System.out.println("\n=> Updating all the documents with {\"student_id\":10001}.");
4953
System.out.println(updateResult);
5054

5155
// findOneAndUpdate
52-
filter = Filters.eq("student_id", 10000);
53-
Bson updateOperation1 = Updates.inc("x", 10); // increment x by 10. As x doesn't exist yet, x=10.
54-
Bson updateOperation2 = Updates.rename("x", "newX"); // rename variable "x" in "newX".
55-
Bson updateOperation3 = Updates.mul("newX", 2); // multiply "newX" by 2.
56-
Bson updateOperation4 = Updates.addToSet("comments",
57-
"This comment is uniq"); // creating an array with a comment.
58-
Bson updateOperation5 = Updates.addToSet("comments",
59-
"This comment is uniq"); // array behave as a set so adding the same comment has no effect.
60-
List<Bson> updates = Arrays.asList(updateOperation1, updateOperation2, updateOperation3, updateOperation4,
61-
updateOperation5);
62-
// findOneAndUpdate returns the old version of the document before the update.
56+
filter = eq("student_id", 10000);
57+
Bson update1 = inc("x", 10); // increment x by 10. As x doesn't exist yet, x=10.
58+
Bson update2 = rename("class_id", "new_class_id"); // rename variable "class_id" in "new_class_id".
59+
Bson update3 = mul("scores.0.score", 2); // multiply the first score in the array by 2.
60+
Bson update4 = addToSet("comments", "This comment is uniq"); // creating an array with a comment.
61+
Bson update5 = addToSet("comments", "This comment is uniq"); // using addToSet so no effect.
62+
Bson updates = combine(update1, update2, update3, update4, update5);
63+
// returns the old version of the document before the update.
6364
Document oldVersion = gradesCollection.findOneAndUpdate(filter, updates);
64-
System.out.println("\nFindOneAndUpdate operation. Printing the old version by default:");
65+
System.out.println("\n=> FindOneAndUpdate operation. Printing the old version by default:");
6566
System.out.println(oldVersion.toJson(prettyPrint));
6667

6768
// but I can also request the new version
68-
filter = Filters.eq("student_id", 10002);
69+
filter = eq("student_id", 10001);
6970
FindOneAndUpdateOptions optionAfter = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
7071
Document newVersion = gradesCollection.findOneAndUpdate(filter, updates, optionAfter);
71-
System.out.println("\nFindOneAndUpdate operation. But we can also ask for the new version of the doc:");
72+
System.out.println("\n=> FindOneAndUpdate operation. But we can also ask for the new version of the doc:");
7273
System.out.println(newVersion.toJson(prettyPrint));
7374
}
7475
}

0 commit comments

Comments
 (0)