BAEL-5371: Create a new module java-mongodb-2 in persistence-modules and add push and set operations in Same MongoDB Update (#11924)

This commit is contained in:
Kapil Khandelwal 2022-03-15 01:04:49 +05:30 committed by GitHub
parent aa35c80c41
commit f74056e1d7
7 changed files with 185 additions and 1 deletions

View File

@ -0,0 +1,5 @@
.classpath
.project
.settings
target
build

View File

@ -0,0 +1,5 @@
## MongoDB
This module contains articles about MongoDB in Java.

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-mongodb-2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>java-mongodb-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>de.flapdoodle.embedmongo</groupId>
<artifactId>de.flapdoodle.embedmongo</artifactId>
<version>${flapdoodle.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongo.version}</version>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>core</artifactId>
<version>${morphia.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<mongo.version>3.12.1</mongo.version>
<flapdoodle.version>1.11</flapdoodle.version>
<morphia.version>1.5.3</morphia.version>
</properties>
</project>

View File

@ -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<Document> 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();
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -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<Document> 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());
}
}

View File

@ -43,6 +43,7 @@
<module>java-jpa-2</module> <!-- long running -->
<module>java-jpa-3</module>
<module>java-mongodb</module> <!-- long running -->
<module>java-mongodb-2</module> <!-- long running -->
<module>jnosql</module> <!-- long running -->
<module>jooq</module>
<module>jpa-hibernate-cascade-type</module>
@ -104,4 +105,4 @@
<postgresql.version>42.2.20</postgresql.version>
</properties>
</project>
</project>