Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用用于Mongodb的java驱动程序,如何在同一字段上搜索多个值?_Mongodb_Mongodb Java - Fatal编程技术网

使用用于Mongodb的java驱动程序,如何在同一字段上搜索多个值?

使用用于Mongodb的java驱动程序,如何在同一字段上搜索多个值?,mongodb,mongodb-java,Mongodb,Mongodb Java,我对mongo有些陌生。我想在一个字段上查询多个值。 在SQL中,我需要如下内容: select * from table where field in ("foo","bar") 假设我在mongodb中有以下文档 { "_id":"foo" } { "_id":"bar" } 我只想模拟这个查询: db.coll.find( { _id: { $in: [ "foo", "bar" ] } } ); 我想检索其_id为“foo”或“bar”的所有文档。 我想使用java驱动程序

我对mongo有些陌生。我想在一个字段上查询多个值。 在SQL中,我需要如下内容:

select * from table where field in ("foo","bar")
假设我在mongodb中有以下文档

{
  "_id":"foo"
}
{
  "_id":"bar"
}
我只想模拟这个查询:

db.coll.find( { _id: { $in: [ "foo", "bar" ] } } );
我想检索其_id为“foo”或“bar”的所有文档。 我想使用java驱动程序来实现这一点

我试过类似的东西

BasicDBObject query = new DBObject()
query.append("_id","foo");
query.append("_id","bar");
collection.find(query);
但这似乎只返回“bar”文档


请帮助

使用操作符中的$in,使用QueryBuilder创建如下查询可能更容易:

select * from table where field in ("foo","bar")
QueryBuilder qb=newquerybuilder();
qb.put(“_id”).in(新字符串[]{“foo”,“bar”});
collection.find(qb.get());
或者更干净一点:

dbobjectquery=QueryBuilder.start(“_id”).in(新字符串[]{“foo”,“bar”}).get();
收集、查找(查询);

我还发现我可以通过编写新的BasicDBObject()来实现它