MongoDB Java Driver Tutorial From the Beginning

בתאריך 11 דצמבר, 2014

Getting Started in MongoDB Java Driver (Windows, Eclipse, Java):

MongoDB Java Driver Tutorial From the Beginning

 

http://ravdeveloper.wordpress.com/mongodb-java-driver-tutorial-from-the-beginning/

MongoDB Java Driver Tutorial From the Beginning

Thank G-D

Hello World…

My name is Yehuda Ginsburg and This is My first Blog for Java developer etc.

 

Getting Started in MongoDB (Windows, Eclipse, Java):

Download

A. first of all Download and Install MongoDB at  https://www.mongodb.org/downloads

A1. download mongo-java-driver at http://central.maven.org/maven2/org/mongodb/mongo-java-driver/

choose the latest driver

download the .jar file

Add data/db folders

B. create a folder under main Drive “data”, under “data” create a folder “db”

like this  C:\data\db

Run the server

C. go to the folder MongoDB installed and under “bin” folder runmongod.exe

like this C:\Program Files\MongoDB 2.6 Standard\bin

C1.notice

 i’m using windows 7 32 bit, Sometimes it just does not run, due to the forced shutdown of the computer or hibernate feature, I could not really understand why…
In case if you run mongod.exe and the window just closes try to run the command prompt under the following script:
“C: / Program Files / MongoDB 2.6 Standard / bin / mongod.exe ”  –repair

and then run mongod.exe again.

 

C2. Running Shell (Optional)

under “bin” run mongo.exe

D. Create new Project in Eclipse

create new java project, name it harav.mongodb,

press “next”  in java setting (the next window) go to libraries tab, press add external jars,

go to your Downloads and select the .jar driver

click finish.

E. MongoDB CRUD example

A. Create a sample java file  Person.java

B. Create MongoCRUD.java like this sample MongoCRUD.java

getting Client

String server = “localhost”;
int port = 27017;
MongoClient client = new MongoClient(server, port);

 

getting  Database (Schema)

String dbName = “myFirstDB”;

DB myDB = client.getDB(dbName);

 

getting Collection (Table)

String collectionName = “myCollection”;

DBCollection collection = myDB.getCollection(collectionName);

 

inserting Person to collection

Person person = new Person(“Yehuda”, “Ginsburg”);

String name = person.getName();
String lastName = person.getLastName();

BasicDBObject document = new BasicDBObject();
document.put(“name”, name);
document.put(“lastName”, lastName);

collection.insert(document);

ObjectId added automatically

 

getting the whole Person (with id from mongo)

DBObject dbObject = collection.findOne(document );

Object id = dbObject.get(“_id”);
String name = dbObject.get(“name”) + “”;
String lastName = dbObject.get(“lastName”) + “”;

Person newPerson = new Person();
newPerson.setName(name);
newPerson.setLastName(lastName);
newPerson.setId(id);

 

getting a List where some field (Key – Row name) equals some Value (Row data)

for example where “name” equals “Yehuda”

String key = “name;

Object value = “Yehuda”;

List<Person> persons = new ArrayList<Person>();
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put(key, value);
DBCursor cursor = collection.find(whereQuery);
while (cursor.hasNext()) {
DBObject dbObject = cursor.next();

Object id = dbObject.get(“_id”);
String name = dbObject.get(“name”) + “”;
String lastName = dbObject.get(“lastName”) + “”;

Person person = new Person(name, lastName);
person.setId(id);
persons.add(person);
}

 

getting a List where few Keys equals some Values

for example where “name” equals “Yehuda” and “lastName” equals “Ginsburg”

String keyOne=name“; Object valueOne=”Yehuda”;

String keyTwo=”lastName”; Object valueTwo=”Ginsburg”;

List<Person> persons = new ArrayList<Person>();

List<BasicDBObject> obj = new ArrayList();
BasicDBObject andQuery = new BasicDBObject();

obj.add(new BasicDBObject(keyOne, valueOne));
obj.add(new BasicDBObject(keyTwo, valueTwo));

andQuery.put(“$and”, obj);

DBCursor cursor = collection.find(andQuery);
while (cursor.hasNext()) {
DBObject dbObject = cursor.next();

Object id = dbObject.get(“_id”);
String name = dbObject.get(“name”) + “”;
String lastName = dbObject.get(“lastName”) + “”;

Person person = new Person(name, lastName);
person.setId(id);

persons.add(person);

}

updating a document

for example changing “lastName” from “Ginsburg” to “Gin”

where “name” equals “Yehuda”

String changeKey=”lastName”;

Object changeValue=”Gin”;

BasicDBObject change = new BasicDBObject();
BasicDBObject setWithChange = new BasicDBObject();

change.append(changeKey, changeValue);
setWithChange.append(“$set”, change);

String whereKey = “name”;

Object whereValue = “Yehuda”;

BasicDBObject where = new BasicDBObject();

where.append(whereKey, whereValue);

collection.update(where, setWithChange);

 

remove data in specific field (column)

BasicDBObject document = new BasicDBObject();
document.put(“name”, “Yehuda”);
collection.remove(document);

remove all data in specific collection

collection.remove(new BasicDBObject());

 

C.Start mongo it…

 

Thanks a lot for the famous website stackoverflow.com

and for Mkyong.com That helps me in this Tutorial

Be well,

Yehuda Ginsburg

 
מאמרים נוספים...