[英]Mongo DB query in java
I have to write a complex mongo query using java but am not able to do it. 我必须使用java编写一个复杂的mongo查询,但我无法做到。
The mongo query looks like this: mongo查询如下所示:
db.video.findOne( {
$or: [
{ key1: { $in : [764] } },
{ key2: {$in : [list2] } },
{ $and [ { key2 : 3}, {key4:67} ] }
]
})
I have to write the above query using the QueryBuilder class. 我必须使用QueryBuilder类编写上述查询。 In what way can I do it? 我该怎么办?
Thanks 谢谢
Using QueryBuilder your query should look like this 使用QueryBuilder,您的查询应如下所示
DBObject query = QueryBuilder.start().or(
QueryBuilder.start("key1").in(764).get(),
QueryBuilder.start("key2").in(keys).get(),
QueryBuilder.start().and("key3").is(3).and("key4").is(64).get()
).get();
Consider using jongo (an API over mongo-java-driver) you can simply copy/paste queries from the shell : 考虑使用jongo (一个基于mongo-java-driver的API),您可以简单地从shell复制/粘贴查询:
collection.findOne("{$or:[{key1: {$in:[764]}},{key2:{$in:[#]}}, {$and:[{key3:3},{key4:67}]}]}", keys).as(People.class);
I had the same problem and i got a solution in another way : 我有同样的问题,我以另一种方式得到了一个解决方案:
ArrayList orList = new ArrayList();
ArrayList andList = new ArrayList();
orList.add(new BasicDBObject("key1", new BasicDBObject("$in", 764)));
orList.add(new BasicDBObject("key2", new BasicDBObject("$in", list2)));
andList.add(new BasicDBObject("key2", 3));
andList.add(new BasicDBObject("key4", 67));
orList.add(new BasicDBObject("$and", andList));
BasicDBObject query = new BasicDBObject("$or", orList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.