Question

I can match starting of string i.e clo with keywords and it gives me correct result db.post.find({"keywords":"/^clo/"}).pretty() When I tried to write same query using spring mongo.It not working properly. It gives result as % string %. i.e. matches anywhere in string. I am trying to match only at starting . my code is

String pattern = "/^" + keyword + "/";
Criteria criteria2 = Criteria.where("keywords").is(keyword).regex(pattern);

Where I am missing ?

Was it helpful?

Solution

  1. You can do it like this:

    Query.query(Criteria.where("keywords").regex("^clo"))

  2. Or use it as native query:

    new BasicQuery("{'keywords' : '/^clo/'}")

Method is() provides the full equals, regex() has to be without / wrappers.

That's is your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top