Thursday 28 November 2013

Example of Data retrivel from MondoDB using JAVA.


Data retrivel from MondoDB using JAVA.

public static void getData(){
DBCursor cursor = null;
MongoClient mongoClient= null;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
DB db = mongoClient.getDB( "mongoDB" );
DBCollection documents = db.getCollection("Documents");


// Get all documents of a collections.
cursor = documents.find();
while(cursor.hasNext()) {
System.out.println(" Get all documents of a collections. \n");
   System.out.println(cursor.next());
}

// Get documents of a collections where age = 25.
//      key is case sensitive so it wont retrieve any documents.
// Key has to "Age" and not "age"
BasicDBObject caseSenQuery = new BasicDBObject();
caseSenQuery.put("age", 25);
cursor = documents.find(caseSenQuery);
System.out.println("\n Get documents of a collections where age = 25 as  key is case sensitive so it wont retrieve any documents. \n");
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

// Get documents of a collections where age = 25.
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("Age", 25);
cursor = documents.find(whereQuery);
while(cursor.hasNext()) {
System.out.println("\n Get documents of a collections where age = 25. \n");
   System.out.println(cursor.next());
}


//   Get documents of a collection by adding mutliple clauses like name and age.
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("Age", 25));
obj.add(new BasicDBObject("Name" , "abcdefxyz"));
andQuery.put("$and", obj);

cursor = documents.find(andQuery);
while (cursor.hasNext()) {
System.out.println("\n Get documents of a collection by adding mutliple clauses like name and age. \n");
System.out.println(cursor.next());
}


//       Get documents where name like pattern for "name"
BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("Name", new BasicDBObject("$regex", "abcd") .append("$options", "i"));
cursor = documents.find(regexQuery);
while (cursor.hasNext()) {
System.out.println("\n Get documents where name like pattern for name. \n");
System.out.println(cursor.next());
}


//      Get documents where name like pattern for "BooksRead.name"
BasicDBObject regexQuery1 = new BasicDBObject();
regexQuery.put("BooksRead.name", new BasicDBObject("$regex", "Krishna") .append("$options", "i"));
cursor = documents.find(regexQuery1);
while (cursor.hasNext()) {
System.out.println("\n Get documents where name like pattern for BooksRead.name. \n");
System.out.println(cursor.next());
}

//     Get documents by quering the embedded/sub document address.area = Aundh.
BasicDBObject embeddedQuery = new BasicDBObject();
embeddedQuery.put("address.area", "Aundh");
cursor = documents.find(embeddedQuery);
while(cursor.hasNext()) {
System.out.println("\n Get documents by quering the embedded/sub docuement address.area = Aundh. \n");
   System.out.println(cursor.next());
}


//   Get documents by quering the embedded/sub document BooksRead.publishedBy = Mehata Publishing Ltd.
BasicDBObject embeddedQuery1 = new BasicDBObject();
embeddedQuery1.put("BooksRead.publishedBy", "Mehata Publishing Ltd");
cursor = documents.find(embeddedQuery1);
while(cursor.hasNext()) {
System.out.println("\n Get documents by quering the embedded/sub docuement BooksRead.publishedBy = Mehata Publishing Ltd. \n");
   System.out.println(cursor.next());
}


// Find documents by date of birth
Date gtDate = null;
try {
gtDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("1984-05-010T8:30:00.000Z");
} catch (ParseException e) {
e.printStackTrace();
}
BasicDBObject dateQueryObj = new BasicDBObject("DateOfBirth",  new BasicDBObject("$lt", gtDate));
cursor = documents.find(dateQueryObj);
while(cursor.hasNext()) {
System.out.println("\n Find documents by date of birth. \n");
   System.out.println(cursor.next());
}


//    Get selective fields from matched document.
BasicDBObject selectiveFields = new BasicDBObject();
selectiveFields.put("Age", 25);
BasicDBObject fields = new BasicDBObject();
fields.put("Name", 1);
fields.put("Age", 2);
fields.put("DateOfBirth", 3);
fields.put("address.area", 3);

cursor = documents.find(selectiveFields, fields);
while (cursor.hasNext()) {
System.out.println("\n Get selective fields from matched document. \n");
System.out.println(cursor.next());
}


//    Get selective fields along with embedded fields from matched document.
BasicDBObject clauseField = new BasicDBObject();
selectiveFields.put("Age", 25);
BasicDBObject embeddedfields = new BasicDBObject();
embeddedfields.put("Name", 1);
embeddedfields.put("Age", 2);
embeddedfields.put("DateOfBirth", 3);
embeddedfields.put("address.area", 4);
embeddedfields.put("BooksRead.name", 5);

cursor = documents.find(clauseField, embeddedfields);
while (cursor.hasNext()) {
System.out.println("\n Get selective fields along with embedded fields from matched document. \n");
System.out.println(cursor.next());
}
}



the output on the console is ::

 Get all documents of a collections.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get documents of a collections where age = 25 as  key is case sensitive so it wont retrieve any documents.


 Get documents of a collections where age = 25.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get documents of a collection by adding mutliple clauses like name and age.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get documents where name like pattern for name.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get documents where name like pattern for BooksRead.name.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get documents by quering the embedded/sub docuement address.area = Aundh.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get documents by quering the embedded/sub docuement BooksRead.publishedBy = Mehata Publishing Ltd.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Find documents by date of birth.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Id" : 12345 , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "Interests" : [ "Reading" , "Politics" , "Basketball"] , "address" : { "addressDetail" : "flat no 333, Q Wing, Sita Nagar" , "area" : "Aundh" , "city" : "Pune" , "pincode" : 411017 , "state" : "MAHARASHTRA" , "country" : "INDIA"} , "BooksRead" : [ { "name" : "The Immortals of Meluha" , "authorName" : "Amish Tripathi" , "publishedBy" : "Westland Press"} , { "name" : "The Krishna Key" , "authorName" : "Ashwin Sanghi" , "publishedBy" : "Westland Ltd"} , { "name" : "Sita: An Illustrated Retelling of Ramayana" , "authorName" : "Devdutt Pattanaik" , "publishedBy" : "Mehata Publishing Ltd"}]}

 Get selective fields from matched document.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "address" : { "area" : "Aundh"}}

 Get selective fields along with embedded fields from matched document.

{ "_id" : { "$oid" : "5296e987910919e6768c9391"} , "Name" : "abcdefxyz" , "Age" : 25 , "DateOfBirth" : { "$date" : "1984-05-08T03:00:00.000Z"} , "address" : { "area" : "Aundh"} , "BooksRead" : [ { "name" : "The Immortals of Meluha"} , { "name" : "The Krishna Key"} , { "name" : "Sita: An Illustrated Retelling of Ramayana"}]}


No comments:

Post a Comment