Tuesday 7 January 2014

Example of MongoDB with MongoJack

There is need of java mapper to the mongo object and MongoJack is very helpful in the same
to achieve the objective. It uses Jackson as the Java JSON mapper.

You need to add the following jars.

1.  bson4jackson-2.1.0.jar
2.  commons-io-2.4.jar
3.  hamcrest-core-1.3.jar
4.  hamcrest-library-1.3.jar
5.  jackson-annotations-2.1.2.jar
6.  jackson-core-2.1.3.jar
7.  jackson-databind-2.1.3.jar
8.  mongo-java-driver-2.11.3.jar
9.  mongojack-2.0.0-RC5.jar
10. persistence-api-1.0.2.jar


In the example you will find how add, update, updating Embedded field, find and find with some criteria using the MongoJack.


public static void main(String[] args) {
create();
findOne();
updateEmbeddedField();
update();
findOne();
findWithCriteria();
}

public static void create(){

DBCollection dbCollection = getCollection();

JacksonDBCollection<Person, String> coll = JacksonDBCollection.wrap(dbCollection, Person.class,
       String.class);
Person person = getPerson();
WriteResult<Person, String> result = coll.insert(person);
Person p = coll.findOne();
Address ad = p.getAddress();
System.out.println("Create o/p ->>  name :: " + p.getName() + " age :: " + p.getAge() + " area :: "+ ad.getArea());
}

public static void updateEmbeddedField(){

DBCollection dbCollection = getCollection();
JacksonDBCollection<Person, String> coll = JacksonDBCollection.wrap(dbCollection, Person.class,
       String.class);

DBUpdate.Builder builder = new DBUpdate.Builder();
builder.set("address.area", "Pune");

Person result = coll.findAndModify(DBQuery.is("name", "Jamse Cook"), null, null, false, builder, false, false);
Address ad = result.getAddress();
System.out.println("updateEmbeddedField o/p ->> name :: " + result.getName() + " age :: " + result.getAge() + " area :: "+ ad.getArea());
}

public static void update(){

DBCollection dbCollection = getCollection();
JacksonDBCollection<Person, String> coll = JacksonDBCollection.wrap(dbCollection, Person.class,
       String.class);

DBUpdate.Builder builder = new DBUpdate.Builder();
builder.set("name", "Abhijit");

Person result = coll.findAndModify(DBQuery.is("name", "Jamse Cook"), null, null, false, builder, false, false);
Address ad = result.getAddress();
System.out.println("update o/p  ->>  name :: " + result.getName() + " age :: " + result.getAge() + " area :: "+ ad.getArea());
}

public static void findOne(){

DBCollection dbCollection = getCollection();
JacksonDBCollection<Person, String> coll = JacksonDBCollection.wrap(dbCollection, Person.class,
       String.class);
Person result = coll.findOne();
Address ad = result.getAddress();
System.out.println("find o/p ->> name :: " + result.getName() + " age :: " + result.getAge() + " area :: "+ ad.getArea());

}

public static void findWithCriteria(){

DBCollection dbCollection = getCollection();
JacksonDBCollection<Person, String> coll = JacksonDBCollection.wrap(dbCollection, Person.class,
       String.class);
Person result = coll.findOne(DBQuery.is("name", "Abhijit"));
Address ad = result.getAddress();
System.out.println("findWithCriteria o/p ->>  name :: " + result.getName() + " age :: " + result.getAge() + " area :: "+ ad.getArea());

}

private static DBCollection getCollection() {
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "jack" );
DBCollection dbCollection = db.getCollection("test");
return dbCollection;
}

private static Person getPerson(){

Person person = new Person();
person.setName("Jamse Cook");
person.setAge(29);

Address address = new Address();
address.setArea("Area 1");
address.setCity("New York 1");
address.setStreet("Aurthor Street 1");
address.setCountry("UK");

person.setAddress(address);

return person;
}

The output on the java console is :


Create o/p ->>  name :: Jamse Cook age :: 29 area :: Area 1
find o/p ->> name :: Jamse Cook age :: 29 area :: Area 1
updateEmbeddedField o/p ->> name :: Jamse Cook age :: 29 area :: Area 1
update o/p  ->>  name :: Jamse Cook age :: 29 area :: Pune
find o/p ->> name :: Abhijit age :: 29 area :: Pune
findWithCriteria o/p ->>  name :: Abhijit age :: 29 area :: Pune

And the output on the mongo shell is :


db.test.find().pretty();
{
"_id" : ObjectId("52cce4e19109c56d473c46eb"),
"address" : {
"area" : "Pune",
"city" : "New York 1",
"country" : "UK",
"street" : "Aurthor Street 1"
},
"age" : 29,
"name" : "Abhijit"
}






3 comments:

  1. MongoClient mongoClient = null;
    try {
    mongoClient = new MongoClient( "localhost" , 27017 );
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    DB db = mongoClient.getDB( "jack" );

    If it throws an exception, will the mongoClient be non-null?

    ReplyDelete
    Replies
    1. Are you new to Java? your question is hilarious :)

      Delete
    2. It throws the exception & the mongoClient object will be null.

      Delete