Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

[Mongo] How to use $in on string array with elements that match a regex?

$
0
0
Hi,

I'm trying to see if an array of strings contains any elements that match a regex using the Criteria object.

Criteria.regex(String) works fine for single-valued string fields, and Criteria.in(Object) works fine for string array fields, but how do I combine in() with regex()? If I pass a string containing a regex, I get no matches. Here's a snippet of the mongo log, showing that the regex is not slashy (look for $in operators below):
Code:

INFO: find: seeded.profile { "$and" : [ { "$or" : [ { "displayName" : { "$regex" : ".*steve@smith\\.com.*" , "$options" : "i"}} , { "title" : { "$regex" : ".*steve@smith\\.com.*" , "$options" : "i"}} , { "primaryField" : { "$regex" : ".*steve@smith\\.com.*" , "$options" : "i"}} , { "user.username" : { "$regex" : ".*steve@smith\\.com.*" , "$options" : "i"}} , { "user.person.givenName" : { "$regex" : ".*steve@smith\\.com.*" , "$options" : "i"}} , { "user.person.surnames" : { "$in" : [ ".*steve@smith\\.com.*"]}} , { "user.person.middles" : { "$in" : [ ".*steve@smith\\.com.*"]}} , { "user.person.emails" : { "$in" : [ ".*steve@smith\\.com.*"]}}]}]}
The java source for the above query uses Criteria created like this (where regex is a string containing the regular expression):
Code:

where("user.person.surnames").in(regex)
This appears to be supported by Mongo since version 1.3.4: https://jira.mongodb.org/browse/SERVER-322

Should there be a Criteria.inRegex(String... regexes) method?

Thanks,
Matthew

Viewing all articles
Browse latest Browse all 128

Trending Articles