minor cleanup
This commit is contained in:
parent
71c298276f
commit
61fa5ff781
|
@ -7,47 +7,47 @@ import com.mongodb.DBCursor;
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
|
|
||||||
public class MongoExample {
|
public class MongoExample {
|
||||||
public static void main( String[] args ) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
MongoClient mongoClient = new MongoClient("localhost", 27017);
|
MongoClient mongoClient = new MongoClient("localhost", 27017);
|
||||||
|
|
||||||
DB database = mongoClient.getDB("myMongoDb");
|
DB database = mongoClient.getDB("myMongoDb");
|
||||||
|
|
||||||
// print existing databases
|
// print existing databases
|
||||||
mongoClient.getDatabaseNames().forEach(System.out::println);
|
mongoClient.getDatabaseNames().forEach(System.out::println);
|
||||||
|
|
||||||
database.createCollection("customers", null);
|
database.createCollection("customers", null);
|
||||||
|
|
||||||
// print all collections in customers database
|
// print all collections in customers database
|
||||||
database.getCollectionNames().forEach(System.out::println);
|
database.getCollectionNames().forEach(System.out::println);
|
||||||
|
|
||||||
// create data
|
// create data
|
||||||
DBCollection collection = database.getCollection("customers");
|
DBCollection collection = database.getCollection("customers");
|
||||||
BasicDBObject document = new BasicDBObject();
|
BasicDBObject document = new BasicDBObject();
|
||||||
document.put("name", "Shubham");
|
document.put("name", "Shubham");
|
||||||
document.put("company", "Baeldung");
|
document.put("company", "Baeldung");
|
||||||
collection.insert(document);
|
collection.insert(document);
|
||||||
|
|
||||||
// update data
|
// update data
|
||||||
BasicDBObject query = new BasicDBObject();
|
BasicDBObject query = new BasicDBObject();
|
||||||
query.put("name", "Shubham");
|
query.put("name", "Shubham");
|
||||||
BasicDBObject newDocument = new BasicDBObject();
|
BasicDBObject newDocument = new BasicDBObject();
|
||||||
newDocument.put("name", "John");
|
newDocument.put("name", "John");
|
||||||
BasicDBObject updateObject = new BasicDBObject();
|
BasicDBObject updateObject = new BasicDBObject();
|
||||||
updateObject.put("$set", newDocument);
|
updateObject.put("$set", newDocument);
|
||||||
collection.update(query, updateObject);
|
collection.update(query, updateObject);
|
||||||
|
|
||||||
// read data
|
// read data
|
||||||
BasicDBObject searchQuery = new BasicDBObject();
|
BasicDBObject searchQuery = new BasicDBObject();
|
||||||
searchQuery.put("name", "John");
|
searchQuery.put("name", "John");
|
||||||
DBCursor cursor = collection.find(searchQuery);
|
DBCursor cursor = collection.find(searchQuery);
|
||||||
while (cursor.hasNext()) {
|
while (cursor.hasNext()) {
|
||||||
System.out.println(cursor.next());
|
System.out.println(cursor.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete data
|
// delete data
|
||||||
BasicDBObject deleteQuery = new BasicDBObject();
|
BasicDBObject deleteQuery = new BasicDBObject();
|
||||||
deleteQuery.put("name", "John");
|
deleteQuery.put("name", "John");
|
||||||
collection.remove(deleteQuery);
|
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 AppIntegrationTest {
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,72 +0,0 @@
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue