title: Form MongoDB Complex Query Using Java
body: <p>Want to form below MongoDB query using java driver, i have tried couple ways but i couldn't make.</p>
  
  <pre><code>**MongoDB Shell:**
  db.collection.find( 
  { "$and" : [ { "abc" : 1} ,
      { $or : [
                { $and : [ { "id.filed1" : "01234" }, { " id.filed2 " : "0123456789" } ] },
                { $and : [ { " id.filed1" : "02222" }, { " id.filed2" : "0422220098" } ] },  
                { $and : [ { " id.filed1" : "01112" }, { " id.filed2" : "0444440005" } ] }
              ]
      } ]})
  
  
    Equavlient SQL: 
    Select * from table 
    where visibility = 1 
    AND (
            (filed1= “01234” AND filed2 = “0123456789”) 
         Or (filed1= “01122” AND filed2 = “0408100098”) 
         Or (filed1= “01122” AND filed2 = “0415000005”)
        )
  
  
     public static void main(String[] args) {
  
            String filed11="01234";
            String filed21 ="0123456789";
            String filed12="02222";
            String filed22="0422220098"; 
            String filed13="01112";
            String filed23="0444440005";
  
            List &lt;UserDetail&gt; ls= new ArrayList&lt;serDetail&gt;();
           UserDetail user1= new UserDetail(filed11,filed21);
           UserDetail user2= new UserDetail (filed12,filed22);
           UserDetail user3= new UserDetail (filed13,filed23);
            ls.add(user1);
            ls.add(user2);
            ls.add(user3);
            req.setSpmUserDetail(ls);
  
  
            BasicDBObject whereClause = new BasicDBObject();
            BasicDBObject orQuery = new BasicDBObject();
            BasicDBObject andQuery = new BasicDBObject();
            List&lt;BasicDBObject&gt; andInsideAndQuery = new ArrayList&lt;BasicDBObject&gt;();
            List&lt;BasicDBObject&gt; objAnd = new ArrayList&lt;BasicDBObject&gt;();
            List&lt;BasicDBObject&gt; objOr = new ArrayList&lt;BasicDBObject&gt;();
  
            objAnd.add(new BasicDBObject(CommonConstants.abc, 1));
  
  
            **for (UserDetail userdata: req.UserDetail ()) {
            andInsideAndQuery.add(new BasicDBObject(“filed1”, userdata.filed1()));
            andInsideAndQuery.add(new BasicDBObject(“filed2”, userdata.filed2()));
            orQuery.put("$or", andQuery);
            andQuery.put("$and", andInsideAndQuery);
            }
     orQuery.put("$or", andQuery);
      objAnd.add(orQuery);**
  
           whereClause.put("$and", objAnd);
            System.out.println(whereClause);
     }
  </code></pre>
  
  <p>Am not able to form in correct mongodb query using Java.</p>
  
  <p>Getting result like this.</p>
  
  <pre><code>   { "$and" : [ { "visibility" : 1} , { "$or" : [ 
      { "$and" : [ { "idmapping.clientid" : "01234"} , { "idmapping.platforminternalid" : "0123456789"} , 
      { "idmapping.clientid" : "01122"} , { "idmapping.platforminternalid" : "0408100098"} , 
      { "idmapping.clientid" : "01122"} , { "idmapping.platforminternalid" : "0415000005"}]}]}]}
  </code></pre>
  
  <p>Thank you in advance!
  Bharathiraja S</p>