我必须用Java运行这个查询
db.Users.find({"name": /^ind/i})
我的Java代码是
Document findQuery = new Document();
findQuery.append("name", Pattern.compile("/^"+name+"/i"));
FindIterable<Document> iterable = db.getCollection("Users").find(findQuery);
它没有返回任何数据,我认为上面的java代码正在转换
> /^ind/i into "/^ind/i"
提前谢谢.
Edit:
根据斯特里比雪夫的建议,更新了查询及其工作状态
db.Users.find({"name": {"$regex": /^ind/, "$options": "i"}})
Java代码
Document regQuery = new Document();
regQuery.append("$regex", "^(?)" + Pattern.quote(name));
regQuery.append("$options", "i");
Document findQuery = new Document();
findQuery.append("name", regQuery);
FindIterable<Document> iterable = db.getCollection("Users").find(findQuery);