Skip to content

Commit 7b433e7

Browse files
committed
Create data and insert document to mongodb
1 parent 734a414 commit 7b433e7

File tree

7 files changed

+136
-10
lines changed

7 files changed

+136
-10
lines changed

data/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>data</artifactId>
7+
<version>${root.version}</version>
8+
9+
<parent>
10+
<artifactId>Mongodb-Java-Tutorials</artifactId>
11+
<groupId>com.hungcdev</groupId>
12+
<version>1.0</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.projectlombok</groupId>
18+
<artifactId>lombok</artifactId>
19+
<version>${lombok.version}</version>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.hungcdev.data;
2+
3+
import lombok.*;
4+
5+
import java.util.List;
6+
7+
@Data
8+
@Builder
9+
@Getter
10+
@Setter
11+
public class Post {
12+
private String id;
13+
private String title;
14+
private String user;
15+
private String content;
16+
private List<String> tags;
17+
private boolean enable;
18+
19+
}

mongodb-connection/pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>mongodb-connection</artifactId>
7+
58
<parent>
69
<artifactId>Mongodb-Java-Tutorials</artifactId>
710
<groupId>com.hungcdev</groupId>
811
<version>1.0</version>
912
</parent>
10-
<modelVersion>4.0.0</modelVersion>
11-
12-
<artifactId>mongodb-connection</artifactId>
1313

1414
<dependencies>
1515
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
1616
<dependency>
1717
<groupId>org.mongodb</groupId>
1818
<artifactId>mongo-java-driver</artifactId>
19-
<version>3.12.6</version>
19+
<version>${mongodb.java.driver.version}</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.projectlombok</groupId>
24+
<artifactId>lombok</artifactId>
25+
<version>${lombok.version}</version>
2026
</dependency>
2127
</dependencies>
2228

mongodb-connection/src/main/java/com/hungcdev/connection/MongoDbConnection.java renamed to mongodb-connection/src/main/java/com/hungcdev/connection/MongoDBConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
1212
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
1313

14-
public class MongoDbConnection {
14+
public class MongoDBConnection {
1515
MongoClient mongoClient;
1616

1717
public void connectMongoDB() {
@@ -45,7 +45,7 @@ public void setMongoClient(MongoClient mongoClient) {
4545
}
4646

4747
public static void main(String[] args) {
48-
MongoDbConnection mongoDbConnection = new MongoDbConnection();
48+
MongoDBConnection mongoDbConnection = new MongoDBConnection();
4949
mongoDbConnection.connectMongoDBWithPOJOs("localhost", 27017);
5050
MongoDatabase mongoDatabase = mongoDbConnection.getMongoClient().getDatabase("HungcDev");
5151
mongoDatabase.createCollection("User");

mongodb-insert/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>mongodb-insert</artifactId>
7+
8+
<parent>
9+
<artifactId>Mongodb-Java-Tutorials</artifactId>
10+
<groupId>com.hungcdev</groupId>
11+
<version>1.0</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.hungcdev</groupId>
17+
<artifactId>data</artifactId>
18+
<version>${root.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.hungcdev</groupId>
22+
<artifactId>mongodb-connection</artifactId>
23+
<version>${root.version}</version>
24+
<scope>compile</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.hungcdev.insert;
2+
3+
import com.hungcdev.connection.MongoDBConnection;
4+
import com.hungcdev.data.Post;
5+
import com.mongodb.client.MongoCollection;
6+
import com.mongodb.client.MongoDatabase;
7+
8+
import java.util.List;
9+
import java.util.UUID;
10+
11+
public class InsertDocument {
12+
private MongoDBConnection mongoDBConnection;
13+
14+
private MongoDatabase mongoDatabase;
15+
16+
public InsertDocument(String ip, int port, String databaseName) {
17+
this.mongoDBConnection = new MongoDBConnection();
18+
this.mongoDBConnection.connectMongoDBWithPOJOs(ip, port);
19+
this.mongoDatabase = mongoDBConnection.getMongoClient().getDatabase(databaseName);
20+
}
21+
22+
public <T> void insert(String collectionName, T document , Class<T> classType) {
23+
MongoCollection<T> collection = mongoDatabase.getCollection(collectionName, classType);
24+
collection.insertOne(document);
25+
}
26+
27+
public static void main(String[] args) {
28+
InsertDocument insertDocument = new InsertDocument("localhost", 27017, "HungcDev");
29+
Post post = Post.builder()
30+
.id(UUID.randomUUID().toString())
31+
.title("Mongodb Java tutorial")
32+
.user("Hungcdev")
33+
.content("Huong dan intert Document trong MongoDB")
34+
.tags(List.of("Mongodb", "Mongodb Java"))
35+
.enable(true)
36+
.build();
37+
insertDocument.insert("Post", post, Post.class);
38+
System.out.println("Insert Post Successful!");
39+
}
40+
41+
42+
43+
}

pom.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
76
<groupId>com.hungcdev</groupId>
87
<artifactId>Mongodb-Java-Tutorials</artifactId>
98
<packaging>pom</packaging>
109
<version>1.0</version>
10+
1111
<modules>
1212
<module>mongodb-connection</module>
13-
<module>mongodb-insertion</module>
14-
<module>mongodb-query</module>
15-
<module>mongodb-update</module>
1613
<module>data</module>
14+
<module>mongodb-insert</module>
1715
</modules>
1816

17+
<properties>
18+
<maven.compiler.source>11</maven.compiler.source>
19+
<maven.compiler.target>11</maven.compiler.target>
20+
21+
<root.version>1.0</root.version>
22+
23+
<mongodb.java.driver.version>3.12.6</mongodb.java.driver.version>
24+
<lombok.version>1.18.12</lombok.version>
25+
</properties>
1926

2027
</project>

0 commit comments

Comments
 (0)