使用Java形成MongoDB复杂查询

时间:2017-07-11 17:51:55

标签: java mongodb mongodb-query mongo-java mongo-java-driver

想要使用java驱动程序在MongoDB查询下面进行查询,我尝试了几种方法,但我无法做到。

**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 <UserDetail> ls= new ArrayList<serDetail>();
         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<BasicDBObject> andInsideAndQuery = new ArrayList<BasicDBObject>();
          List<BasicDBObject> objAnd = new ArrayList<BasicDBObject>();
          List<BasicDBObject> objOr = new ArrayList<BasicDBObject>();

          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);
   }

无法使用Java在正确的mongodb查询中形成。

得到这样的结果。

   { "$and" : [ { "visibility" : 1} , { "$or" : [ 
    { "$and" : [ { "idmapping.clientid" : "01234"} , { "idmapping.platforminternalid" : "0123456789"} , 
    { "idmapping.clientid" : "01122"} , { "idmapping.platforminternalid" : "0408100098"} , 
    { "idmapping.clientid" : "01122"} , { "idmapping.platforminternalid" : "0415000005"}]}]}]}

提前谢谢! Bharathiraja S

1 个答案:

答案 0 :(得分:1)

您可以尝试以下查询。

请注意在for循环中推送List<BasicDBObject> andInsideAndQuery = new ArrayList<BasicDBObject>();的更改,这会创建多个$and表达式。

BasicDBObject whereClause = new BasicDBObject();
BasicDBObject orQuery = new BasicDBObject();
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> objOr = new ArrayList<BasicDBObject>();
List<BasicDBObject> objAnd = new ArrayList<BasicDBObject>();

for (UserDetail userdata : req.UserDetail()) {
     List<BasicDBObject> andInsideAndQuery = new ArrayList<BasicDBObject>();
     andInsideAndQuery.add(new BasicDBObject("filed1", userdata.filed1()));
     andInsideAndQuery.add(new BasicDBObject("filed2", userdata.filed2()));
     andQuery.put("$and", andInsideAndQuery);
     objOr.add(andQuery);
}

objAnd.add(new BasicDBObject(CommonConstants.abc, 1));
orQuery.put("$or", objOr);
objAnd.add(orQuery);

whereClause.put("$and", objAnd);

旁注 - 内部$and表达式是多余的,可以删除。所以两者

{ $and : [ { "id.filed1" : "01234" }, { " id.filed2 " : "0123456789" } ] }

相同

{ "id.filed1": "01234", "id.filed2": "0123456789" }