Monday, 30 November 2015

BasicDBObject is a basic implementation of bson object.

BasicDBObject is a basic implementation of bson object. BSON is a binary-encoded serialization of JSON-like documents.

BSON was designed to have the following characteristics.
        1. Lightweight : Occupies less space
        2. Traversable : BSON is designed to be traversed easily.
        3. Efficient : Encoding data to BSON and decoding from BSON can be performed very quickly in most languages due to the use of C data types.

package com.orient.kalyan.hadoop.training;

import com.mongodb.BasicDBObject;

public class BasicDBObjectExample {
 public static void main(String args[]){
  BasicDBObject employee = new BasicDBObject();
  BasicDBObject address = new BasicDBObject();
  
  employee.put("id", 1);
  employee.put("firstName", "Hari krishna");
  employee.put("lastName", "Gurram");
  
  address.put("city", "Bangalore");
  address.put("area", "Marthalli");
  address.put("PIN", "560037");
  
  employee.put("address", address);
  
  System.out.println(address);
  System.out.println(employee);
  
 }
}


Output
{ "city" : "Bangalore" , "area" : "Marthalli" , "PIN" : "560037"}
{ "id" : 1 , "firstName" : "Hari krishna" , "lastName" : "Gurram" , "address" : { "city" : "Bangalore" , "area" : "Marthalli" , "PIN" : "560037"}}


You can convert a map to BasicDBObject, by simply passing the map to BasicDBObject constructor.

package com.orient.kalyan.hadoop.training;

import java.util.HashMap;
import java.util.Map;
import com.mongodb.BasicDBObject;

public class BasicDBObjectExample {
 public static void main(String args[]){ 
  Map<String, Object> emp = new HashMap<String, Object> ();
  Map<String, String> addr = new HashMap<String, String> ();
  
  emp.put("id", 1);
  emp.put("firstName", "Hari krishna");
  emp.put("lastName", "Gurram");
  
  addr.put("city", "Bangalore");
  addr.put("area", "Marthalli");
  addr.put("PIN", "560037");
  
  emp.put("address", addr);
  
  BasicDBObject employee = new BasicDBObject(emp);
  
  System.out.println(employee);  
 }
}

Output
{ "firstName" : "Hari krishna" , "lastName" : "Gurram" , "address" : { "area" : "Marthalli" , "PIN" : "560037" , "city" : "Bangalore"} , "id" : 1}

Related Posts Plugin for WordPress, Blogger...