Search This Blog

Monday 16 June 2014

Read-Write to MongoDB via java

In the last post I used the java driver to connect to the MongoDB. Now to read the documents from the collection:
public static String DB_NAME = "fruits";
   public static String COLLECTION_NAME = "fruits";

   public static void main(String[] args) throws UnknownHostException {
      Mongo mongoClient = new Mongo();
      DB targetDB = mongoClient.getDB(DB_NAME);
      
      DBCollection coll = targetDB.getCollection(COLLECTION_NAME);
      DBObject myDoc = coll.findOne();
      System.out.println(myDoc);
      
      mongoClient.close();
   }
The output is:
{ "_id" : { "$oid" : "52d305ab4e386a0b85ac249a"} , "name" : "apple" ,
 "color" : "red" , "rating" : 4.0 , "weight" : 2.3}
The collection object has a findOne method. It returns the first document in the collection.
I decided to try and fetch all the documents in the collection:
public static void main(String[] args) throws UnknownHostException {
      Mongo mongoClient = new Mongo();
      DB targetDB = mongoClient.getDB(DB_NAME);

      DBCollection collection = targetDB.getCollection(COLLECTION_NAME);
      DBCursor cursor = collection.find();
      
      int i = 1;
      while (cursor.hasNext()) {
         System.out.println((i++) + " : " + cursor.next());
      }
      cursor.close();

      mongoClient.close();
   }
The find method of the collection will return a cursor. The output records is:
1 : { "_id" : { "$oid" : "52d305ab4e386a0b85ac249a"} , "name" : "apple" , "color" : "red" , 
"rating" : 4.0 , "weight" : 2.3}
2 : { "_id" : { "$oid" : "52d30d464e386a0b85ac249b"} , "name" : "lost" , "age" : 12.0}
3 : { "_id" : { "$oid" : "52d30d524e386a0b85ac249c"} , "name" : "INS Viraat" ,
 "wt" : 5000.0 , "displacement" : 190.87}
I decided to fetch records using query language.
public static void main(String[] args) throws UnknownHostException {
      Mongo mongoClient = new Mongo();
      DB targetDB = mongoClient.getDB(DB_NAME);

      DBCollection coll = targetDB.getCollection(COLLECTION_NAME);
      BasicDBObject query = new BasicDBObject("name", "lost");
      DBObject myDoc = coll.findOne(query);
      System.out.println(myDoc);

      mongoClient.close();
   }
Here we have used a BasicDBObject to perform the query testing.The BasicDBObject is used to create a json styled key value object. The query fetched the expected record:
{ "_id" : { "$oid" : "52d30d464e386a0b85ac249b"} , "name" : "lost" , "age" : 12.0}

To query on multiple paramaters:
BasicDBObject query = new BasicDBObject("name", "apple");
query.append("age", 14);
DBObject myDoc = coll.findOne(query);
The append function will add additional "and" conditions.Thus a document will be returned if its name is apple and rating is 4.0 Ill need to look into querying in-depth.For now I am going to try and insert a record into the fruit collection. The below code will add a new fruit to the collection:
public static void main(String[] args) throws UnknownHostException {
      BasicDBObject newFruit = new BasicDBObject("name", "mango");
      newFruit.append("color", "yellow");
      newFruit.append("rating", 2);
      newFruit.append("weight", 1.03);
     
      Mongo mongoClient = new Mongo();
      DB targetDB = mongoClient.getDB(DB_NAME);
      DBCollection collection = targetDB.getCollection(COLLECTION_NAME);
      collection.insert(newFruit);
      mongoClient.close();
   }
The insert method of the DBCollection object will take care of the insertion.  Next would be to do update and delete.

No comments:

Post a Comment