I have to run this query in Java
db.Users.find({"name": /^ind/i})
My Java code is
Document findQuery = new Document();
findQuery.append("name", Pattern.compile("/^"+name+"/i"));
FindIterable<Document> iterable = db.getCollection("Users").find(findQuery);
It is not returning any data, I think the above java code is converting
> /^ind/i into "/^ind/i"
Thanks in advance.
Edit:
Based on stribizhev suggestion updated the query and its worked
db.Users.find({"name": {"$regex": /^ind/, "$options": "i"}})
Java code
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);
"^(?i)"+Pattern.quote(name)instead of"/^"+name+"/i".findQuery.append("name", new BsonRegularExpression(regexLiteral));