Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -54,22 +54,22 @@
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${javax.xml.bind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${javax.xml.bind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -77,4 +77,4 @@
|
||||
<netty-transport-version>4.1.71.Final</netty-transport-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
@@ -0,0 +1,5 @@
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
target
|
||||
build
|
||||
@@ -0,0 +1,5 @@
|
||||
## MongoDB
|
||||
|
||||
This module contains articles about MongoDB in Java.
|
||||
|
||||
|
||||
@@ -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>
|
||||
+54
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
+53
@@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,18 @@
|
||||
<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>
|
||||
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.baeldung.ordering.caseinsensitive;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.*;
|
||||
import com.mongodb.client.model.Collation;
|
||||
import com.mongodb.client.model.Projections;
|
||||
import com.mongodb.client.model.Sorts;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.MongoDBContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mongodb.client.model.Aggregates.project;
|
||||
import static com.mongodb.client.model.Aggregates.sort;
|
||||
import static com.mongodb.client.model.Sorts.ascending;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@Testcontainers
|
||||
class CaseInsensitiveOrderingLiveTest {
|
||||
|
||||
private static MongoCollection<Document> userCollections;
|
||||
|
||||
@Container
|
||||
private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
|
||||
|
||||
@BeforeAll
|
||||
private static void setup() {
|
||||
|
||||
MongoClient mongoClient = new MongoClient(mongoDBContainer.getContainerIpAddress(), mongoDBContainer.getMappedPort(27017));
|
||||
MongoDatabase database = mongoClient.getDatabase("test");
|
||||
userCollections = database.getCollection("users");
|
||||
|
||||
List<Document> list = new ArrayList<>();
|
||||
list.add(Document.parse("{'name': 'ben', surname: 'ThisField' }"));
|
||||
list.add(Document.parse("{'name': 'aen', surname: 'Does' }"));
|
||||
list.add(Document.parse("{'name': 'Ben', surname: 'Not' }"));
|
||||
list.add(Document.parse("{'name': 'cen', surname: 'Matter' }"));
|
||||
list.add(Document.parse("{'name': 'Aen', surname: 'Really' }"));
|
||||
list.add(Document.parse("{'name': 'Cen', surname: 'TrustMe' }"));
|
||||
|
||||
userCollections.insertMany(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMongoCollection_whenUsingFindWithSort_caseIsConsideredByDefault() {
|
||||
FindIterable<Document> nameDoc = userCollections.find().sort(ascending("name"));
|
||||
MongoCursor<Document> cursor = nameDoc.cursor();
|
||||
|
||||
List<String> expectedNamesOrdering = Arrays.asList("Aen", "Ben", "Cen", "aen", "ben", "cen");
|
||||
List<String> actualNamesOrdering = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
Document document = cursor.next();
|
||||
actualNamesOrdering.add(document.get("name").toString());
|
||||
}
|
||||
|
||||
assertEquals(expectedNamesOrdering, actualNamesOrdering);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMongoCollection_whenUsingFindWithSortAndCollation_caseIsNotConsidered() {
|
||||
FindIterable<Document> nameDoc = userCollections.find().sort(ascending("name"))
|
||||
.collation(Collation.builder().locale("en").build());
|
||||
MongoCursor<Document> cursor = nameDoc.cursor();
|
||||
List<String> expectedNamesOrdering = Arrays.asList("aen", "Aen", "ben", "Ben", "cen", "Cen");
|
||||
List<String> actualNamesOrdering = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
Document document = cursor.next();
|
||||
actualNamesOrdering.add(document.get("name").toString());
|
||||
}
|
||||
|
||||
assertEquals(expectedNamesOrdering, actualNamesOrdering);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMongoCollection_whenUsingFindWithSortAndAggregate_caseIsNotConsidered() {
|
||||
|
||||
Bson projectBson = project(
|
||||
Projections.fields(
|
||||
Projections.include("name", "surname"),
|
||||
Projections.computed("lowerName", Projections.computed("$toLower", "$name"))));
|
||||
|
||||
AggregateIterable<Document> nameDoc = userCollections.aggregate(
|
||||
Arrays.asList(projectBson,
|
||||
sort(Sorts.ascending("lowerName"))));
|
||||
|
||||
MongoCursor<Document> cursor = nameDoc.cursor();
|
||||
|
||||
List<String> expectedNamesOrdering = Arrays.asList("aen", "Aen", "ben", "Ben", "cen", "Cen");
|
||||
List<String> actualNamesOrdering = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
Document document = cursor.next();
|
||||
actualNamesOrdering.add(document.get("name").toString());
|
||||
}
|
||||
|
||||
assertEquals(expectedNamesOrdering, actualNamesOrdering);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -36,4 +36,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
+1
@@ -19,6 +19,7 @@ public class TodoDatasourceConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConfigurationProperties("spring.datasource.todos.hikari")
|
||||
public DataSource todosDataSource() {
|
||||
return todosDataSourceProperties()
|
||||
.initializeDataSourceBuilder()
|
||||
|
||||
+1
@@ -3,6 +3,7 @@ spring.datasource.todos.url=jdbc:h2:mem:todos
|
||||
spring.datasource.todos.username=sa
|
||||
spring.datasource.todos.password=null
|
||||
spring.datasource.todos.driverClassName=org.h2.Driver
|
||||
spring.datasource.todos.hikari.connectionTimeout=44444
|
||||
spring.datasource.topics.url=jdbc:h2:mem:topics
|
||||
spring.datasource.topics.username=sa
|
||||
spring.datasource.topics.password=null
|
||||
|
||||
@@ -29,4 +29,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
@@ -107,22 +107,22 @@
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${javax.xml.bind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${javax.xml.bind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>${com.sun.xml.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
Reference in New Issue
Block a user