Added java-mongodb module (#978)
* Made maven project. * Added mongo driver dependencies. * Added crud operations. * Class rename * Added tests
This commit is contained in:
parent
66917a2877
commit
e27131f3a5
|
@ -0,0 +1,5 @@
|
|||
.classpath
|
||||
.project
|
||||
.settings
|
||||
target
|
||||
build
|
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-mongodb</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mongo.version>3.4.1</mongo.version>
|
||||
<flapdoodle.version>1.11</flapdoodle.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<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>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
public class MongoExample {
|
||||
public static void main( String[] args ) {
|
||||
|
||||
MongoClient mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
DB database = mongoClient.getDB("myMongoDb");
|
||||
|
||||
// print existing databases
|
||||
mongoClient.getDatabaseNames().forEach(System.out::println);
|
||||
|
||||
database.createCollection("customers", null);
|
||||
|
||||
// print all collections in customers database
|
||||
database.getCollectionNames().forEach(System.out::println);
|
||||
|
||||
// create data
|
||||
DBCollection collection = database.getCollection("customers");
|
||||
BasicDBObject document = new BasicDBObject();
|
||||
document.put("name", "Shubham");
|
||||
document.put("company", "Baeldung");
|
||||
collection.insert(document);
|
||||
|
||||
// update data
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
query.put("name", "Shubham");
|
||||
BasicDBObject newDocument = new BasicDBObject();
|
||||
newDocument.put("name", "John");
|
||||
BasicDBObject updateObject = new BasicDBObject();
|
||||
updateObject.put("$set", newDocument);
|
||||
collection.update(query, updateObject);
|
||||
|
||||
// read data
|
||||
BasicDBObject searchQuery = new BasicDBObject();
|
||||
searchQuery.put("name", "John");
|
||||
DBCursor cursor = collection.find(searchQuery);
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
|
||||
// delete data
|
||||
BasicDBObject deleteQuery = new BasicDBObject();
|
||||
deleteQuery.put("name", "John");
|
||||
collection.remove(deleteQuery);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.Mongo;
|
||||
import de.flapdoodle.embedmongo.MongoDBRuntime;
|
||||
import de.flapdoodle.embedmongo.MongodExecutable;
|
||||
import de.flapdoodle.embedmongo.MongodProcess;
|
||||
import de.flapdoodle.embedmongo.config.MongodConfig;
|
||||
import de.flapdoodle.embedmongo.distribution.Version;
|
||||
import de.flapdoodle.embedmongo.runtime.Network;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
private static final String DB_NAME = "myMongoDb";
|
||||
private MongodExecutable mongodExe;
|
||||
private MongodProcess mongod;
|
||||
private Mongo mongo;
|
||||
private DB db;
|
||||
private DBCollection collection;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
|
||||
// Creating Mongodbruntime instance
|
||||
MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
|
||||
|
||||
// Creating MongodbExecutable
|
||||
mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345,
|
||||
Network.localhostIsIPv6()));
|
||||
|
||||
// Starting Mongodb
|
||||
mongod = mongodExe.start();
|
||||
mongo = new Mongo("localhost", 12345);
|
||||
|
||||
// Creating DB
|
||||
db = mongo.getDB(DB_NAME);
|
||||
|
||||
// Creating collection Object and adding values
|
||||
collection = db.getCollection("customers");
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws Exception {
|
||||
mongod.stop();
|
||||
mongodExe.cleanup();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressPersistance() {
|
||||
|
||||
BasicDBObject contact = new BasicDBObject();
|
||||
contact.put("name", "John");
|
||||
contact.put("company", "Baeldung");
|
||||
|
||||
// Inserting document
|
||||
collection.insert(contact);
|
||||
DBCursor cursorDoc = collection.find();
|
||||
BasicDBObject contact1 = new BasicDBObject();
|
||||
while (cursorDoc.hasNext()) {
|
||||
contact1 = (BasicDBObject) cursorDoc.next();
|
||||
System.out.println(contact1);
|
||||
}
|
||||
assertEquals(contact1.get("name"), "John");
|
||||
}
|
||||
}
|
8
pom.xml
8
pom.xml
|
@ -1,6 +1,5 @@
|
|||
<?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">
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
@ -175,6 +174,7 @@
|
|||
<module>xml</module>
|
||||
<module>xmlunit2</module>
|
||||
<module>xstream</module>
|
||||
</modules>
|
||||
<module>java-mongodb</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
</project>
|
Loading…
Reference in New Issue