bael-5848 - How to Insert a HashMap Into MongoDB With Java (#12938)
* bael-5848 - new module * bael-5848 - tests
This commit is contained in:
parent
1a4cc86a08
commit
548c69164f
@ -64,6 +64,7 @@
|
|||||||
<module>spring-boot-persistence-h2</module>
|
<module>spring-boot-persistence-h2</module>
|
||||||
<module>spring-boot-persistence-mongodb</module>
|
<module>spring-boot-persistence-mongodb</module>
|
||||||
<module>spring-boot-persistence-mongodb-2</module>
|
<module>spring-boot-persistence-mongodb-2</module>
|
||||||
|
<module>spring-boot-persistence-mongodb-3</module>
|
||||||
<module>spring-data-arangodb</module>
|
<module>spring-data-arangodb</module>
|
||||||
<module>spring-data-cassandra</module>
|
<module>spring-data-cassandra</module>
|
||||||
<module>spring-data-cassandra-test</module>
|
<module>spring-data-cassandra-test</module>
|
||||||
@ -115,4 +116,4 @@
|
|||||||
<testcontainers.version>1.16.3</testcontainers.version>
|
<testcontainers.version>1.16.3</testcontainers.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -7,4 +7,4 @@
|
|||||||
- [Count Documents Using Spring Data MongoDB Repository](https://www.baeldung.com/spring-data-mongodb-count)
|
- [Count Documents Using Spring Data MongoDB Repository](https://www.baeldung.com/spring-data-mongodb-count)
|
||||||
- [Spring Data MongoDB – Configure Connection](https://www.baeldung.com/spring-data-mongodb-connection)
|
- [Spring Data MongoDB – Configure Connection](https://www.baeldung.com/spring-data-mongodb-connection)
|
||||||
- [Connect to Multiple Databases Using Spring Data MongoDB](https://www.baeldung.com/mongodb-multiple-databases-spring-data)
|
- [Connect to Multiple Databases Using Spring Data MongoDB](https://www.baeldung.com/mongodb-multiple-databases-spring-data)
|
||||||
- More articles: [[<--prev]](../spring-boot-persistence-mongodb)
|
- More articles: [[<--prev]](../spring-boot-persistence-mongodb) [[next-->]](../spring-boot-persistence-mongodb-3)
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
# Relevant Articles
|
||||||
|
|
||||||
|
- More articles: [[<--prev]](../spring-boot-persistence-mongodb-2)
|
@ -0,0 +1,34 @@
|
|||||||
|
<?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>spring-boot-persistence-mongodb-3</artifactId>
|
||||||
|
<name>spring-boot-persistence-mongodb-3</name>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<description>This is simple boot application for Spring boot persistence mongodb</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>de.flapdoodle.embed</groupId>
|
||||||
|
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.boot.hashmap;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootHashMapApplication {
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
SpringApplication.run(SpringBootHashMapApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
spring.application.name=spring-boot-persistence-mongodb-3
|
@ -0,0 +1,120 @@
|
|||||||
|
package com.baeldung.boot.hashmap;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@DirtiesContext
|
||||||
|
@TestPropertySource("/embedded.properties")
|
||||||
|
class MongoDbHashMapIntegrationTest {
|
||||||
|
|
||||||
|
private static final Map<String, Object> MAP = new HashMap<>();
|
||||||
|
private static final Set<Map<String, Object>> MAP_SET = new HashSet<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoTemplate mongo;
|
||||||
|
|
||||||
|
private void assertHasMongoId(Map<String, Object> saved) {
|
||||||
|
assertNotNull(saved);
|
||||||
|
assertNotNull(saved.get("_id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void init() {
|
||||||
|
MAP.put("name", "Document A");
|
||||||
|
MAP.put("number", 2);
|
||||||
|
MAP.put("dynamic", true);
|
||||||
|
|
||||||
|
Map<String, Object> otherMap = new HashMap<>();
|
||||||
|
otherMap.put("name", "Other Document");
|
||||||
|
otherMap.put("number", 22);
|
||||||
|
|
||||||
|
MAP_SET.add(MAP);
|
||||||
|
MAP_SET.add(otherMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingMap_thenInsertSucceeds() {
|
||||||
|
Map<String, Object> saved = mongo.insert(MAP, "map-collection");
|
||||||
|
|
||||||
|
assertHasMongoId(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenMapSet_thenInsertSucceeds() {
|
||||||
|
Collection<Map<String, Object>> saved = mongo.insert(MAP_SET, "map-set");
|
||||||
|
|
||||||
|
saved.forEach(this::assertHasMongoId);
|
||||||
|
assertEquals(2, saved.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMap_whenDocumentConstructed_thenInsertSucceeds() {
|
||||||
|
Document document = new Document(MAP);
|
||||||
|
|
||||||
|
Document saved = mongo.insert(document, "doc-collection");
|
||||||
|
|
||||||
|
assertHasMongoId(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMap_whenBasicDbObjectConstructed_thenInsertSucceeds() {
|
||||||
|
BasicDBObject dbObject = new BasicDBObject(MAP);
|
||||||
|
|
||||||
|
BasicDBObject saved = mongo.insert(dbObject, "db-collection");
|
||||||
|
|
||||||
|
assertHasMongoId(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenObjectList_whenDocumentSetConstructed_thenInsertSucceeds() {
|
||||||
|
Map<String, List<Object>> input = new HashMap<>();
|
||||||
|
List<Object> listOne = new ArrayList<>();
|
||||||
|
listOne.add("Doc A");
|
||||||
|
listOne.add(1);
|
||||||
|
|
||||||
|
List<Object> listTwo = new ArrayList<>();
|
||||||
|
listTwo.add("Doc B");
|
||||||
|
listTwo.add(2);
|
||||||
|
|
||||||
|
input.put("a", listOne);
|
||||||
|
input.put("b", listTwo);
|
||||||
|
|
||||||
|
Set<Document> docs = input.entrySet()
|
||||||
|
.stream()
|
||||||
|
.collect(HashSet<Document>::new, (set, entry) -> {
|
||||||
|
Document document = new Document();
|
||||||
|
|
||||||
|
document.put("_id", entry.getKey());
|
||||||
|
Iterator<Object> iterator = entry.getValue()
|
||||||
|
.iterator();
|
||||||
|
document.put("name", iterator.next());
|
||||||
|
document.put("number", iterator.next());
|
||||||
|
|
||||||
|
set.add(document);
|
||||||
|
}, Set::addAll);
|
||||||
|
|
||||||
|
Collection<Document> saved = mongo.insert(docs, "custom-set");
|
||||||
|
saved.forEach(this::assertHasMongoId);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
spring.mongodb.embedded.version=4.4.9
|
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration scan="true" scanPeriod="15 seconds" debug="false">
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user