此声明在mongo控制台中正常工作:
db.system_integrator.find( { person_name: { $in: [ "Mick Jagger", "Bob Dyla", "Tony Orlando" ] } } )
我正在尝试使用QueryBuilder在java中构建等效的,如下所示:
DBObject query = new BasicDBObject();
query.put(dbFieldName, "[ \"Mick Jagger\", \"Bob Dyla\", \"Tony Orlando\" ]");
qb.in( query );
然而,这种构建查询的方式不起作用。我在这里做错了什么?
答案 0 :(得分:1)
您可以使用Java执行此操作,如下所示:
BasicDBList list = new BasicDBList();
list.add("Mick Jagger");
list.add("Bob Dyla");
list.add("Tony Orlando");
QueryBuilder qb = new QueryBuilder();
qb.put("person_name").in(list);
或
BasicDBList list = new BasicDBList();
list.add("Mick Jagger");
list.add("Bob Dyla");
list.add("Tony Orlando");
DBObject query = new BasicDBObject();
query.put("person_name", new BasicDBObject("$in", list));