Find documents where field is (equals to or not) using eq and ne operator In MongoDb in java




In this mongoDB tutorial we will write java programs to FIND documents where field is (equals to or not) using eq and ne operator In MongoDb in java.

Contents of page >
  • 1) Program to Query Document From Collection In MongoDb in java using eq and ne Operator >
    • 1.1) Program to Query Document From Collection In MongoDb in java using eq (equals) Operator >
    • 1.2) Program to Query Document From Collection In MongoDb in java using ne (not equals) Operator >

1) Program to Query Document From Collection In MongoDb in java using eq and ne Operator >
1.1) Program to Query Document From Collection In MongoDb in java using eq (equals) Operator >
package queryDocumentFromCollectionInMongoDb;
/**
* Write a program to Query Document From Collection In MongoDb in java using Eq And Ne Operator
*/
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 QueryDocumentFromCollectionInMongoDb6_usingEqAndNeOperator {
   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();
          basicDBObject1.put("id", 1);
          basicDBObject1.put("name", "Ankit");
         
          /* Insert document1/record1 in COLLECTION in MongoDB */
          System.out.println("Insert document1");
          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 */
          /** FIND documents where id = 2 */
          BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
                                            //$eq operator is for EQUALS TO
                                            //Pass > $eq as key > and > 2 as value
          basicDBObject_SearchCondition.put("id",
                       new BasicDBObject("$eq", 2));
          /** 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 = 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" : "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 = 2
Display documents of collection after QUERING collection in MongoDB
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
*/


1.2) Program to Query Document From Collection In MongoDb in java using ne (not equals) Operator >

FIND documents where id != 2 From this collection
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
                                                  //$ne operator is for NOT EQUALS TO
                                                  //Pass > $ne as key - and - 2 as value
basicDBObject_SearchCondition.put("id", new BasicDBObject("$ne", 2));
Result
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}



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 :