Search code examples
regexmongodbmongodb-javamongo-java-driver

Mongo DB and Java ends with Regex


I am trying to get all documents from my database where the page path ends with a certain expression using this code:

query.put(pagepath, new BasicDBObject(
"$regex", "/(" +expression+ ")$/"));

and where expression is replaced by values like ".html", ".JSON", "contact/", "/"... I get no errors but no results either, even though, when I test my regex on https://regex101.com/ it works and one of the links at least matchs.

enter image description here


Solution

  • This is the solution : query.put(pagepath, new BasicDBObject("$regex", ".*(" + expression + ")$"));