使用 Java 形成 MongoDB 复杂查询
Form MongoDB Complex Query Using Java
想要使用 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"}]}]}]}
提前致谢!
巴拉提拉贾 S
您可以试试下面的查询。
请注意将 List<BasicDBObject> andInsideAndQuery = new ArrayList<BasicDBObject>();
推入 for 循环的更改,这会创建多个 $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" }
想要使用 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"}]}]}]}
提前致谢! 巴拉提拉贾 S
您可以试试下面的查询。
请注意将 List<BasicDBObject> andInsideAndQuery = new ArrayList<BasicDBObject>();
推入 for 循环的更改,这会创建多个 $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" }