{
"_id" : ObjectId("53ace08c98dea42bb1cb84ba"),
"id" : "xyz",
}
在id = "xyz"
或id = "abc"
如何用Java在mongodb中编写这个查询
答案 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?