简体   繁体   English

$ regex与$ nin

[英]$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. 我需要从集合中获得结果,其中字段user_id(字符串)在其中任何位置都没有abcdef 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. 当我遍历游标时,我发现它仍然允许abcdef作为结果中的子字符串。

Any other way to express the said query? 还有其他表达上述查询的方式吗?

Instead of testing multiple regexes, you can combine several regexes into one: 您可以将多个正则表达式合并为一个,而不是测试多个正则表达式:

/regex1/ or /regex2/

translates to 转换为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM