Domanda

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 ?

È stato utile?

Soluzione

  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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top