|
4 | 4 | import com.mongodb.client.MongoClients;
|
5 | 5 | import com.mongodb.client.MongoCollection;
|
6 | 6 | 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; |
8 | 10 | import com.mongodb.client.result.UpdateResult;
|
9 | 11 | import org.bson.Document;
|
10 | 12 | import org.bson.conversions.Bson;
|
11 | 13 | import org.bson.json.JsonWriterSettings;
|
12 | 14 |
|
13 |
| -import java.util.Arrays; |
14 |
| -import java.util.List; |
15 | 15 | import java.util.logging.Level;
|
16 | 16 | import java.util.logging.Logger;
|
17 | 17 |
|
| 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 | + |
18 | 22 | public class Update {
|
19 | 23 |
|
20 | 24 | public static void main(String[] args) {
|
21 |
| - Logger.getLogger("org.mongodb.driver").setLevel(Level.WARNING); |
| 25 | + Logger.getLogger("org.mongodb.driver").setLevel(Level.ALL); |
22 | 26 | JsonWriterSettings prettyPrint = JsonWriterSettings.builder().indent(true).build();
|
23 | 27 |
|
24 | 28 | try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
|
25 | 29 | MongoDatabase sampleTrainingDB = mongoClient.getDatabase("sample_training");
|
26 | 30 | MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("grades");
|
27 | 31 |
|
28 | 32 | // 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!"); |
31 | 35 | 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."); |
33 | 37 | System.out.println(gradesCollection.find(filter).first().toJson(prettyPrint));
|
34 | 38 | System.out.println(updateResult);
|
35 | 39 |
|
36 | 40 | // 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!"); |
39 | 43 | UpdateOptions options = new UpdateOptions().upsert(true);
|
40 | 44 | 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."); |
42 | 46 | System.out.println(updateResult);
|
43 | 47 | System.out.println(gradesCollection.find(filter).first().toJson(prettyPrint));
|
44 | 48 |
|
45 | 49 | // update many documents
|
46 |
| - filter = Filters.eq("student_id", 10001); |
| 50 | + filter = eq("student_id", 10001); |
47 | 51 | 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}."); |
49 | 53 | System.out.println(updateResult);
|
50 | 54 |
|
51 | 55 | // 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. |
63 | 64 | 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:"); |
65 | 66 | System.out.println(oldVersion.toJson(prettyPrint));
|
66 | 67 |
|
67 | 68 | // but I can also request the new version
|
68 |
| - filter = Filters.eq("student_id", 10002); |
| 69 | + filter = eq("student_id", 10001); |
69 | 70 | FindOneAndUpdateOptions optionAfter = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
|
70 | 71 | 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:"); |
72 | 73 | System.out.println(newVersion.toJson(prettyPrint));
|
73 | 74 | }
|
74 | 75 | }
|
|
0 commit comments