Skip to content

Commit e27131f

Browse files
sbmaggarwalmaibin
authored andcommitted
Added java-mongodb module (eugenp#978)
* Made maven project. * Added mongo driver dependencies. * Added crud operations. * Class rename * Added tests
1 parent 66917a2 commit e27131f

5 files changed

Lines changed: 178 additions & 4 deletions

File tree

‎java-mongodb/.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.classpath
2+
.project
3+
.settings
4+
target
5+
build

‎java-mongodb/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>java-mongodb</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<junit.version>4.12</junit.version>
15+
<mongo.version>3.4.1</mongo.version>
16+
<flapdoodle.version>1.11</flapdoodle.version>
17+
</properties>
18+
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>${junit.version}</version>
25+
<scope>test</scope>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>de.flapdoodle.embedmongo</groupId>
30+
<artifactId>de.flapdoodle.embedmongo</artifactId>
31+
<version>${flapdoodle.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
36+
<dependency>
37+
<groupId>org.mongodb</groupId>
38+
<artifactId>mongo-java-driver</artifactId>
39+
<version>${mongo.version}</version>
40+
</dependency>
41+
42+
</dependencies>
43+
44+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung;
2+
3+
import com.mongodb.BasicDBObject;
4+
import com.mongodb.DB;
5+
import com.mongodb.DBCollection;
6+
import com.mongodb.DBCursor;
7+
import com.mongodb.MongoClient;
8+
9+
public class MongoExample {
10+
public static void main( String[] args ) {
11+
12+
MongoClient mongoClient = new MongoClient("localhost", 27017);
13+
14+
DB database = mongoClient.getDB("myMongoDb");
15+
16+
// print existing databases
17+
mongoClient.getDatabaseNames().forEach(System.out::println);
18+
19+
database.createCollection("customers", null);
20+
21+
// print all collections in customers database
22+
database.getCollectionNames().forEach(System.out::println);
23+
24+
// create data
25+
DBCollection collection = database.getCollection("customers");
26+
BasicDBObject document = new BasicDBObject();
27+
document.put("name", "Shubham");
28+
document.put("company", "Baeldung");
29+
collection.insert(document);
30+
31+
// update data
32+
BasicDBObject query = new BasicDBObject();
33+
query.put("name", "Shubham");
34+
BasicDBObject newDocument = new BasicDBObject();
35+
newDocument.put("name", "John");
36+
BasicDBObject updateObject = new BasicDBObject();
37+
updateObject.put("$set", newDocument);
38+
collection.update(query, updateObject);
39+
40+
// read data
41+
BasicDBObject searchQuery = new BasicDBObject();
42+
searchQuery.put("name", "John");
43+
DBCursor cursor = collection.find(searchQuery);
44+
while (cursor.hasNext()) {
45+
System.out.println(cursor.next());
46+
}
47+
48+
// delete data
49+
BasicDBObject deleteQuery = new BasicDBObject();
50+
deleteQuery.put("name", "John");
51+
collection.remove(deleteQuery);
52+
}
53+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import com.mongodb.BasicDBObject;
8+
import com.mongodb.DB;
9+
import com.mongodb.DBCollection;
10+
import com.mongodb.DBCursor;
11+
import com.mongodb.Mongo;
12+
import de.flapdoodle.embedmongo.MongoDBRuntime;
13+
import de.flapdoodle.embedmongo.MongodExecutable;
14+
import de.flapdoodle.embedmongo.MongodProcess;
15+
import de.flapdoodle.embedmongo.config.MongodConfig;
16+
import de.flapdoodle.embedmongo.distribution.Version;
17+
import de.flapdoodle.embedmongo.runtime.Network;
18+
19+
public class AppTest {
20+
21+
private static final String DB_NAME = "myMongoDb";
22+
private MongodExecutable mongodExe;
23+
private MongodProcess mongod;
24+
private Mongo mongo;
25+
private DB db;
26+
private DBCollection collection;
27+
28+
@Before
29+
public void setup() throws Exception {
30+
31+
// Creating Mongodbruntime instance
32+
MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
33+
34+
// Creating MongodbExecutable
35+
mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345,
36+
Network.localhostIsIPv6()));
37+
38+
// Starting Mongodb
39+
mongod = mongodExe.start();
40+
mongo = new Mongo("localhost", 12345);
41+
42+
// Creating DB
43+
db = mongo.getDB(DB_NAME);
44+
45+
// Creating collection Object and adding values
46+
collection = db.getCollection("customers");
47+
}
48+
49+
@After
50+
public void teardown() throws Exception {
51+
mongod.stop();
52+
mongodExe.cleanup();
53+
}
54+
55+
@Test
56+
public void testAddressPersistance() {
57+
58+
BasicDBObject contact = new BasicDBObject();
59+
contact.put("name", "John");
60+
contact.put("company", "Baeldung");
61+
62+
// Inserting document
63+
collection.insert(contact);
64+
DBCursor cursorDoc = collection.find();
65+
BasicDBObject contact1 = new BasicDBObject();
66+
while (cursorDoc.hasNext()) {
67+
contact1 = (BasicDBObject) cursorDoc.next();
68+
System.out.println(contact1);
69+
}
70+
assertEquals(contact1.get("name"), "John");
71+
}
72+
}

‎pom.xml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<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">
43
<modelVersion>4.0.0</modelVersion>
54
<groupId>com.baeldung</groupId>
65
<artifactId>parent-modules</artifactId>
@@ -175,6 +174,7 @@
175174
<module>xml</module>
176175
<module>xmlunit2</module>
177176
<module>xstream</module>
178-
</modules>
177+
<module>java-mongodb</module>
178+
</modules>
179179

180-
</project>
180+
</project>

0 commit comments

Comments
 (0)