How to do $or and "and" in mongo query for java driver?
I want to do both AND
and $or
operations in a single mongo query.
In mongo doc i have read mongo queries use AND
by default.
If i use only or $or
, its work开发者_开发技巧ing. But if i try to use AND
and $or
its not working.
I have tried it like this
BasicDBObject query = new BasicDBObject();
query.append("name", "Anbu");
BasicDBObject orQuery = new BasicDBObject();
orQuery.put("name", "Kalaio");
List<BasicDBObject> orQueries = new ArrayList<BasicDBObject>();
orQueries.add(orQuery);
orQuery = new BasicDBObject();
orQuery.put("name", "Kumar");
orQueries.add(orQuery);
orQuery = new BasicDBObject();
orQuery.put("$or", orQueries);
query.put("$or", orQueries);
DBCursor cur = coll.find(query);//Not Working
DBCursor cur = coll.find(orQuery);//Working
Where i am doing it wrong?
Thanks!
I'm not familiar with the Java driver, but it seems like you're trying to nest an "or". Unfortunately, that's not currently allowed in mongo. It's something planned in the future though. See these bugs for more info:
https://jira.mongodb.org/browse/SERVER-2585
https://jira.mongodb.org/browse/SERVER-1089
精彩评论