使用$或$ in在Java中创建mongodb查询 [英] Creating a mongodb query in Java with $or and $in
本文介绍了使用$或$ in在Java中创建mongodb查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用mongodb api编写一个java代码来创建这个mongodb查询:
I am trying to write a java code using mongodb api to create this mongodb query:
{ "$or": [{"prd" : {"$in" : ["1234", "0987"]}} , {"rsin" : "3228742"}]}
以下是我目前使用的代码:
Here's the code I am working with so far:
QueryBuilder builder = new QueryBuilder();
if (builder == null) {
builder = QueryBuilder.start();
}
if (mongoKey.equals("prd")){
ArrayList<String> vals = new ArrayList<String>();
for (int i=0; i < prdList; i++){
vals.add(prdList.get(i));
}
DBObject obj = new BasicDBObject (mongoKey, new BasicDBObject("$in", vals));
builder.or(obj);
}else {
builder.and(mongoKey).is(mongoValue);
}
目前打印出错误的语法:
This is currently printing out the wrong syntax:
{ "$or": [{"prd" : {"$in" : ["1234", "0987"]}}] , "rsin" : "3228742"}
任何帮助?
推荐答案
问题出在 else
块,你需要使用或
方法而不是和
。
The problem is on else
block, you need to use or
method instead of and
.
...
}else {
builder.or(new BasicDBObject(mongoKey, mongoValue));
}
这将产生您想要的查询。
This will produce the query that you want.
这篇关于使用$或$ in在Java中创建mongodb查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文