Spring MongoDB data criteria using $ in and regex list

I discovered MongoDb functionality that allows you to find items from a list of regular expressions using $ in. Example:

db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )

And it works great when entering directly into the Mongo tool as a request.

Now I am trying to apply this query using spring -data-mongodb version 1.8.4.RELEASE. Code example:

List<Pattern> regexList = Arrays.asList(Pattern.compile("\\b5\\b"), Pattern.compile("\\b8\\b")); //regexes to find numbers 5 or 8 in string
Query query = new Query();
query.addCriteria(Criteria.where("key").in(regexList));

And it doesn’t find anything, because the request looks like this:

{"key" : { "$in" : [ { "$regex" : "\\b5\\b"} , { "$regex" : "\\b8\\b"}]}}}

The first problem is $ regex, which is not resolved in $ in requests. The second problem is quotation marks that span regular expressions. I also tried passing a list of strings, but without success.

Am I missing something?

+4

Source: https://habr.com/ru/post/1650281/


All Articles