From f74056e1d78537e1eb1bceadefa23d9101a4deec Mon Sep 17 00:00:00 2001 From: Kapil Khandelwal Date: Tue, 15 Mar 2022 01:04:49 +0530 Subject: [PATCH] BAEL-5371: Create a new module java-mongodb-2 in persistence-modules and add push and set operations in Same MongoDB Update (#11924) --- persistence-modules/java-mongodb-2/.gitignore | 5 ++ persistence-modules/java-mongodb-2/README.md | 5 ++ persistence-modules/java-mongodb-2/pom.xml | 53 ++++++++++++++++++ .../mongo/update/PustSetOperation.java | 54 +++++++++++++++++++ .../src/main/resources/logback.xml | 13 +++++ .../update/PustSetOperationLiveTest.java | 53 ++++++++++++++++++ persistence-modules/pom.xml | 3 +- 7 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 persistence-modules/java-mongodb-2/.gitignore create mode 100644 persistence-modules/java-mongodb-2/README.md create mode 100644 persistence-modules/java-mongodb-2/pom.xml create mode 100644 persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/PustSetOperation.java create mode 100644 persistence-modules/java-mongodb-2/src/main/resources/logback.xml create mode 100644 persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/update/PustSetOperationLiveTest.java diff --git a/persistence-modules/java-mongodb-2/.gitignore b/persistence-modules/java-mongodb-2/.gitignore new file mode 100644 index 0000000000..79ba317cb5 --- /dev/null +++ b/persistence-modules/java-mongodb-2/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.settings +target +build \ No newline at end of file diff --git a/persistence-modules/java-mongodb-2/README.md b/persistence-modules/java-mongodb-2/README.md new file mode 100644 index 0000000000..1b49e11499 --- /dev/null +++ b/persistence-modules/java-mongodb-2/README.md @@ -0,0 +1,5 @@ +## MongoDB + +This module contains articles about MongoDB in Java. + + diff --git a/persistence-modules/java-mongodb-2/pom.xml b/persistence-modules/java-mongodb-2/pom.xml new file mode 100644 index 0000000000..ffc8da0b64 --- /dev/null +++ b/persistence-modules/java-mongodb-2/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + java-mongodb-2 + 1.0-SNAPSHOT + java-mongodb-2 + + + com.baeldung + persistence-modules + 1.0.0-SNAPSHOT + + + + + de.flapdoodle.embedmongo + de.flapdoodle.embedmongo + ${flapdoodle.version} + test + + + org.mongodb + mongo-java-driver + ${mongo.version} + + + dev.morphia.morphia + core + ${morphia.version} + + + org.testcontainers + mongodb + 1.16.3 + test + + + org.testcontainers + junit-jupiter + 1.16.3 + test + + + + + 3.12.1 + 1.11 + 1.5.3 + + + diff --git a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/PustSetOperation.java b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/PustSetOperation.java new file mode 100644 index 0000000000..bb7eca4f23 --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/PustSetOperation.java @@ -0,0 +1,54 @@ +package com.baeldung.mongo.update; + +import org.bson.Document; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Filters; +import com.mongodb.client.model.Updates; +import com.mongodb.client.result.UpdateResult; + +public class PustSetOperation { + + private static MongoClient mongoClient; + + private static String testCollectionName; + private static String databaseName; + + public static void setUp() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + } + databaseName = "baeldung"; + testCollectionName = "marks"; + } + + public static void pushSetSolution() { + + MongoDatabase database = mongoClient.getDatabase(databaseName); + MongoCollection collection = database.getCollection(testCollectionName); + + Document subjectData = new Document().append("subjectId", 126) + .append("subjectName", "Java Programming") + .append("marks", 70); + UpdateResult updateQueryResult = collection.updateOne(Filters.eq("studentId", 1023), Updates.combine(Updates.set("totalMarks", 170), Updates.push("subjectDetails", subjectData))); + System.out.println("updateQueryResult:- " + updateQueryResult); + + } + + public static void main(String args[]) { + + // + // Connect to cluster (default is localhost:27017) + // + setUp(); + + // + // Push document into the array and set a field + // + pushSetSolution(); + + } +} + diff --git a/persistence-modules/java-mongodb-2/src/main/resources/logback.xml b/persistence-modules/java-mongodb-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/update/PustSetOperationLiveTest.java b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/update/PustSetOperationLiveTest.java new file mode 100644 index 0000000000..6279747429 --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/update/PustSetOperationLiveTest.java @@ -0,0 +1,53 @@ +package com.baeldung.update; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import org.bson.Document; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Filters; +import com.mongodb.client.model.Updates; +import com.mongodb.client.result.UpdateResult; + +public class PustSetOperationLiveTest { + + private static MongoClient mongoClient; + private static MongoDatabase db; + private static MongoCollection collection; + + @BeforeClass + public static void setup() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + db = mongoClient.getDatabase("baeldung"); + collection = db.getCollection("marks"); + + collection.insertOne(Document.parse("{\n" + " \"studentId\": 1023,\n" + " \"studentName\":\"James Broad\",\n" + " \"joiningYear\":\"2018\",\n" + " \"totalMarks\":100,\n" + " \"subjectDetails\":[\n" + + " {\n" + " \"subjectId\":123,\n" + " \"subjectName\":\"Operating Systems Concepts\",\n" + " \"marks\":4,\n" + " },\n" + " {\n" + + " \"subjectId\":124,\n" + " \"subjectName\":\"Numerical Analysis\",\n" + " \"marks\":60\n" + " }\n" + " ]\n" + " }")); + + } + } + + @Test + public void givenMarksCollection_whenPushSetOperation_thenCheckingForDocument() { + + Document subjectData = new Document().append("subjectId", 126) + .append("subjectName", "Java Programming") + .append("marks", 70); + UpdateResult updateQueryResult = collection.updateOne(Filters.eq("studentId", 1023), Updates.combine(Updates.set("totalMarks", 170), Updates.push("subjectDetails", subjectData))); + + Document studentDetail = collection.find(Filters.eq("studentId", 1023)) + .first(); + assertNotNull(studentDetail); + assertFalse(studentDetail.isEmpty()); + + } + +} + diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 64a9519a8b..f8e3cb05e8 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -43,6 +43,7 @@ java-jpa-2 java-jpa-3 java-mongodb + java-mongodb-2 jnosql jooq jpa-hibernate-cascade-type @@ -104,4 +105,4 @@ 42.2.20 - \ No newline at end of file +