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

前端 未结 7 1955
谎友^
谎友^ 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:24

    You need to pass an instance of a Java RegEx (java.util.regex.Pattern):

    BasicDBObject q = new BasicDBObject();
    q.put("name",  java.util.regex.Pattern.compile(m));
    dbc.find(q);
    

    This will be converted to a MongoDB regex when sent to the server, as well as any RegEx flags.

提交回复
热议问题