davneetnarang
davneetnarang

Reputation: 436

$regex with $nin

I need results from my collection where the field user_id (a string) does not have abc or def anywhere in it. Here's what I tried:

$regex_array = array("/abc/", "/def/");
$cursor = $colection->find(array('user_id'=> array('$nin'=> $regex_array)));
$cursor = $colection->find(array('user_id'=> array('$regex'=> array('$nin'=> $regex_array))));

When I iterate over the cursor, I'm finding that it is still allowing abc and def as substrings in the results.

Any other way to express the said query?

Upvotes: 0

Views: 531

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

Instead of testing multiple regexes, you can combine several regexes into one:

/regex1/ or /regex2/

translates to

/(?:regex1)|(?:regex2)/

Upvotes: 2

Related Questions