如何在where子句中使用多个条件在mongodb java中搜索

时间:2014-06-27 03:24:05

标签: java mongodb

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

id = "xyz"id = "abc"

中选择此合集

如何用Java在mongodb中编写这个查询

1 个答案:

答案 0 :(得分:4)

您可以使用$或操作数来执行mongodb查询。

db.col.find({$或:[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());
}

您也可以通过此链接How can I build an $or query for MongoDB using the Java driver?