Reputation: 616
Is there any way to find a document in mongoclient with regular expressions? Something similar to LIKE query in sql
I tried following code, but its not working:
Pattern regex = Pattern.compile("/raj/");
JsonObject query = new JsonObject().put("firstName", regex.toString());
mongoClient.find("users",query, res -> {
if(res.succeeded()){
future.complete(res.result());
}else{
future.fail(res.cause());
}
});
Upvotes: 0
Views: 559
Reputation: 4598
Try like this
JsonObject query = new JsonObject().put("firstName", new JsonObject()
.put("$regex", ".*" + searchKeyword + ".*"
)
);
also in mongo shell this works:
db.users.find({"firstName": { "$regex": ".*raj.*"}})
Upvotes: 2