Friday 6 March 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" } } }

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}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment