Find documents using $in operator In MongoDb in java




In this mongoDB tutorial we will write java programs to FIND documents using IN operator in Collection In MongoDb in java.

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

Program to Query Document From Collection In MongoDb in java using in Operator>
FIND documents where id = 1 or id = 2
package queryDocumentFromCollectionInMongoDb;
/**
* Write a program to Query Document From Collection In MongoDb in java Using In Operator
*/
import java.util.ArrayList;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class QueryDocumentFromCollectionInMongoDb3_UsingInOperator {
   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 BasicDBObject1 (document /record) */
          BasicDBObject basicDBObject1 = new BasicDBObject();
          System.out.println("Insert document1");
          basicDBObject1.put("id", 1);
          basicDBObject1.put("name", "Ankit");
         
          /* Insert document1/record1 in COLLECTION in MongoDB */
          dbCollection.insert(basicDBObject1);
         
          /* create a BasicDBObject2 (document /record) */
          BasicDBObject basicDBObject2 = new BasicDBObject();
          basicDBObject2.put("id", 2);
          basicDBObject2.put("name", "Sam");
         
          /* Insert document2 in collection in MongoDB */
          System.out.println("Insert document2");
          dbCollection.insert(basicDBObject2);        
          /* create a BasicDBObject3 (document /record) */
          BasicDBObject basicDBObject3 = new BasicDBObject();
          basicDBObject3.put("id", 3);
          basicDBObject3.put("name", "Neh");
         
          /* Insert document3 in collection in MongoDB */
          System.out.println("Insert document3");
          dbCollection.insert(basicDBObject3);        
          /* Display documents of collection before QUERING DOCUMENTs in MongoDB */
          System.out.println("\nDisplay collection before QUERING collection in MongoDB");
          DBCursor cursor0 = dbCollection.find();
          while (cursor0.hasNext()) {
                 System.out.println(cursor0.next());
          }
         
          /** At this stage dbCollection looks like this >
               { "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
               { "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
               { "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
          */
         
         
          /** Now, let's FIND the documents from collection in MongoDB */
          //Create list of id's which you want to FIND
          List<Integer> idList = new ArrayList<Integer>();
          idList.add(1);
          idList.add(2);
         
          /** FIND documents where id = 1 or id = 2*/
          BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
                                       //Pass > $in as key > and > idList as value
          basicDBObject_SearchCondition.put("id",
                                     new BasicDBObject("$in", idList));
          /** Execute FIND/SEARCH query - To FIND DOCUMENT in MongoDB */
          //find method Select documents in collection and returns cursor to the selected documents.
          System.out.println("\nExecute FIND query where id = 1 or id = 2 ");
          DBCursor cursor = dbCollection.find(basicDBObject_SearchCondition);
         
          /* Display documents of collection after QUERING collection in MongoDB */
          System.out.println("\nDisplay documents of collection after QUERING collection in MongoDB ");
          while (cursor.hasNext()) {
                 System.out.println(cursor.next());
          }
          /** At this stage dbCollection looks like this >
              { "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
              { "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
          */
         
   }
}
/* OUTPUT
Insert document1
Insert document2
Insert document3
Display collection before QUERING collection in MongoDB
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
Execute FIND query where id = 1 or id = 2
Display documents of collection after QUERING collection in MongoDB
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
*/



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 :