Mongo DB query in java

女生的网名这么多〃 提交于 2019-12-03 11:16:50

Using QueryBuilder your query should look like this

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 :

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!