1
{
  "_id" : ObjectId("53ace08c98dea42bb1cb84ba"),
  "id" : "xyz",
}

select this collection where id = "xyz" or id = "abc"

how to write this query in mongodb by Java

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
sumit
  • 37
  • 4
  • 9
  • Possible duplicate of [How can I build an $or query for MongoDB using the Java driver?](https://stackoverflow.com/questions/10620771/how-can-i-build-an-or-query-for-mongodb-using-the-java-driver) – hestellezg Jun 02 '17 at 18:04

1 Answers1

4

You can use OR clause by using the $or operand to perform mongodb queries.

db.col.find({$or:[clause1, clause2]})

DBObject document1 = new BasicDBObject("id", "abc");  
DBObject document2 = new BasicDBObject("id", "xyz");       

or.add(document1);
or.add(document2);

DBObject query = new BasicDBObject("$or", or);

DBCursor cur=db.getCollection("user").find(query);//user is the collection

while(cur.hasNext()){
System.out.println(cur.next());
}

You can go through this link too How can I build an $or query for MongoDB using the Java driver?

Community
  • 1
  • 1
yogi_bear
  • 313
  • 4
  • 12