How to query mongodb with “like” using the java api?

前端 未结 7 1934
谎友^
谎友^ 2020-12-14 00:59

this question is very similar to another post

I basically want to use the mongodb version of the sql \"like\" \'%m%\' operator

but in my situation i\'m using

7条回答
  •  遥遥无期
    2020-12-14 01:28

    You must first quote your text and then use the compile to get a regex expression:

    q.put("name",  Pattern.compile(Pattern.quote(m)));
    

    Without using java.util.Pattern.quote() some characters are not escaped.

    e.g. using ? as the m parameter will throw an exception.

提交回复
热议问题