Wednesday, 4 March 2015

mongoDB : Java : Get one document


“findOne()” method of DBCollection class returns a single object from this collection.

Step 1: Get Mongo client
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

Step 2: Connect to database
DB db = mongoClient.getDB("sample");

Step 3 : Get collection
DBCollection collection = db.getCollection("employee");
Step 4 : Get one document using findOne() method.
DBObject doc = collection.findOne();

Step 5 : Close client
mongoClient.close();

Sample data

> 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" } } }
{ "_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" } }
 }
{ "_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.DBObject;
import com.mongodb.MongoClient;

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: Get one document */
    DBObject doc = collection.findOne();
    
    System.out.println(doc);
    
    /* Close client */
    mongoClient.close();
  }
}


Output
{ "_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"}}}







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment