Query single Document From Collection In MongoDb in java




In this mongoDB tutorial we will write java programs to Query single Document From Collection In MongoDb in java.

Contents of page >
  • Program to Query single Document From Collection In MongoDb in java >

Program to Query single Document From Collection In MongoDb in java >
package queryDocumentFromCollectionInMongoDb;
/**
* Write a program to Query single Document From Collection In MongoDb in java
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class QueryDocumentFromCollectionInMongoDb9_singleDocument {
   public static void main(String[] args) {
          /* Connect to MongoDB */
          MongoClient mongoClient =
                       new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("Employee");
          /*If collection already exists, you may remove everything
          from collection for perfect OUTPUT of program*/
          dbCollection.remove(new BasicDBObject()); //Not Mandatory
                      
         
          /* create a BasicDBObject (document /record) */
          BasicDBObject basicDBObject1 = new BasicDBObject();
          basicDBObject1.put("id", 1);
          basicDBObject1.put("name", "Ankit");
         
          /* Insert document/record in COLLECTION in MongoDB */
          System.out.println("Insert document1");
          dbCollection.insert(basicDBObject1);
         
          /* create a BasicDBObject (document /record) */
          BasicDBObject basicDBObject2 = new BasicDBObject();
          basicDBObject2.put("id", 2);
          basicDBObject2.put("name", "Sam");
         
          /* Insert document/record in collection in MongoDB */
          System.out.println("Insert document2");
          dbCollection.insert(basicDBObject2);        
         
          /** findOne method returns a single document from collection. */
          DBObject dbObject = dbCollection.findOne();
          /** Display single document from collection */
          System.out.println("\nDisplay single document from collection in MongoDB ");
          System.out.println(dbObject);
         
   }
}
/* OUTPUT
Insert document1
Insert document2
Display single document from collection in MongoDB
{ "_id" : { "$oid" : "585c191c7dbba61f08a7dd8c"} , "id" : 1 , "name" : "Ankit"}
*/
So, we inserted two records but only one record was fetched using findOne method.


Download >
Now, it’s time to Download maven Project :) Click here.

Read : How to Import Maven project in eclipse

OR





Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com(JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.



RELATED LINKS>







eEdit
Must read for you :