Monday 30 November 2015

MongoDB : QueryBuilder: Get all documents if the field exists


“exist()” method of QueryBuilder class used to construct queries like $exist operator.

DBObject doc = QueryBuilder.start().put("age").exists(true).get();


Above statement creates a query like below.

{ "age" : { "$exists" : true}}

query returns all documents, which contains the field "age".


I had below data in employee collection.

> db.employee.find()
{ "_id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram", "salary" : 12345, "hobbies" : [ "writing blogs", "playing cricket", "watching movies", "reading books" ], "address" : { "office" : { "
street" : "Koramangala BDA Complex", "city" : "Bangalore", "state" : "Karnataka", "country" : "India", "PIN" : "560034" }, "home" : { "street" : "Near panchayat office", "city" : "Ongole", "state" : "
Andhra Pradesh", "country" : "India", "PIN" : "523169" } }, "age" : 26 }
{ "_id" : 2, "firstName" : "Rama Krishna", "lastName" : "Gurram", "salary" : 54321, "hobbies" : [ "playing cricket", "reading books", "travelling" ], "address" : { "office" : { "street" : "Rupena Agra
hara", "city" : "Bangalore", "state" : "Karnataka", "country" : "India", "PIN" : "560068" }, "home" : { "street" : "Near panchayat office", "city" : "Ongole", "state" : "Andhra Pradesh", "country" : "
India", "PIN" : "523169" } } }
{ "_id" : 3, "firstName" : "Jigar", "lastName" : "Shah", "salary" : 52456, "hobbies" : [ "travelling", "watching movies" ], "address" : { "office" : { "street" : "TNagar", "city" : "Chennai", "state"
: "Tamilnadu", "country" : "India", "PIN" : "341234" }, "home" : { "street" : "Ganghi Nagar", "city" : "Delhi", "state" : "Delhi", "country" : "India", "PIN" : "110037" } } }
{ "_id" : 4, "firstName" : "Piyush", "lastName" : "Rai", "salary" : 65432, "hobbies" : [ "travelling", "reading philosophy", "climbing hills" ], "address" : { "office" : { "street" : "Ameerpet", "city
" : "Hyderabad", "state" : "Andhra Pradesh", "country" : "India", "PIN" : "564321" }, "home" : { "street" : "BDA street", "city" : "Patna", "state" : "Bihar", "country" : "India", "PIN" : "324123" } }
, "age" : 25 }
{ "_id" : 5, "firstName" : "Keerthi", "lastName" : "Parush", "salary" : 49000, "hobbies" : [ "shopping", "trecking" ], "address" : { "office" : { "street" : "Domlur", "city" : "Bangalore", "state" : "
Karnataka", "country" : "India", "PIN" : "564921" }, "home" : { "street" : "BTM Layout", "city" : "Bangalore", "state" : "Karnataka", "country" : "India", "PIN" : "234135" } } }


package com.orient.kalyan.hadoop.training;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;

public class MongoDBEx {
 /* Step 1 : get mongoCLient */
 public static MongoClient getMongoClient() {
  MongoClient mongoClient = null;
  try {
   mongoClient = new MongoClient("localhost", 27017);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return mongoClient;
 }

 public static void main(String[] args) throws Exception {
  MongoClient mongoClient = getMongoClient();

  /* Step 2: Connect to DB */
  DB db = mongoClient.getDB("test");

  /* Step 3 : Get collection */
  DBCollection collection = db.getCollection("employee");

  /* Step 4: Create a query using QueryBuilder */
  DBObject doc = QueryBuilder.start().put("age").exists(true).get();
  
  System.out.println("Query is\n" + doc + "\n");
  
  DBCursor cursor = collection.find(doc);  
  while(cursor.hasNext()){
   System.out.println(cursor.next());
  }
  
  /* Close client */
  mongoClient.close();
 }
}


Output
Query is
{ "age" : { "$exists" : true}}

{ "_id" : 1.0 , "firstName" : "Hari Krishna" , "lastName" : "Gurram" , "salary" : 12345.0 , "hobbies" : [ "writing blogs" , "playing cricket" , "watching movies" , "reading books"] , "address" : { "office" : { "street" : "Koramangala BDA Complex" , "city" : "Bangalore" , "state" : "Karnataka" , "country" : "India" , "PIN" : "560034"} , "home" : { "street" : "Near panchayat office" , "city" : "Ongole" , "state" : "Andhra Pradesh" , "country" : "India" , "PIN" : "523169"}} , "age" : 26.0}
{ "_id" : 4.0 , "firstName" : "Piyush" , "lastName" : "Rai" , "salary" : 65432.0 , "hobbies" : [ "travelling" , "reading philosophy" , "climbing hills"] , "address" : { "office" : { "street" : "Ameerpet" , "city" : "Hyderabad" , "state" : "Andhra Pradesh" , "country" : "India" , "PIN" : "564321"} , "home" : { "street" : "BDA street" , "city" : "Patna" , "state" : "Bihar" , "country" : "India" , "PIN" : "324123"}} , "age" : 25.0}



MongoDB : How GridFS handles data more than 16MB

The maximum document size in mongoDB is 16MB. To store documents larger than the maximum size (16 MB), MongoDB provides the GridFS API.

How GridFS handles data more than 16MB
Instead of storing a file in a single document, GridFS divides a file into parts, called chunks. Each chunk stored as a single document.  

GridFS uses two collections to store files. One to store meta data about chunks, and other to store chunks. When you query a GridFS store for a file, the driver or client will reassemble the chunks as needed.

package com.orient.kalyan.hadoop.training;

import java.io.File;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class GRID_FS_Example {

 /* Step 1 : get mongoCLient */
 public static MongoClient getMongoClient() {
  MongoClient mongoClient = null;
  try {
   mongoClient = new MongoClient("localhost", 27017);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return mongoClient;
 }

 public static void saveFIle(DB db, File file)throws Exception{
  GridFS gridfs = new GridFS(db, "videos");
  GridFSInputFile gfsFile = gridfs.createFile(file);
  gfsFile.setFilename("myvideo");
  gfsFile.save();
 }
 
 public static void getFile(DB db){
  String newFileName = "myvideo";
  GridFS gfsPhoto = new GridFS(db, "videos");
  GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
  System.out.println(imageForOutput);
 }
 
 
 public static void main(String[] args) throws Exception {

  MongoClient  mongoClient = getMongoClient();
  
  DB db = mongoClient.getDB("test");

  File file = new File("/home/hadoop/work/input/hello.mp4");
  saveFIle(db, file);
  
  getFile(db);
  
  System.out.println("done");
 }
}


Output
{ "filename" : "myvideo" , "aliases" :  null  , "chunkSize" : 261120 , "uploadDate" : { "$date" : "2015-11-24T08:45:25.365Z"} , "length" : 915607173 , "_id" : { "$oid" : "54c35ba5df161e5e1b7a6491"} , "contentType" :  null  , "md5" : "8ce772b47ddbb007a089b103579140dc"}
done


After running above java program, it creates two collections, one for to store chunks and other to store metadata.

> show collections
kalyan
system.indexes
videos
videos.chunks
videos.files

Related Posts Plugin for WordPress, Blogger...