On this page本页内容
$regex¶Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.42 with UTF-8 support.为查询中的模式匹配字符串提供正则表达式功能。MongoDB使用Perl兼容的正则表达式(即“PCRE”)版本8.42,支持UTF-8。
To use 要使用$regex, use one of the following syntaxes:$regex,请使用以下语法之一:
In MongoDB, you can also use regular expression objects (i.e. 在MongoDB中,还可以使用正则表达式对象(即/pattern/) to specify regular expressions:/pattern/)来指定正则表达式:
For restrictions on particular syntax use, see $regex vs. /pattern/ Syntax.有关特定语法使用的限制,请参阅$regex与/pattern/syntax的对比。
$options¶The following 以下<options> are available for use with regular expression.<options>可用于正则表达式。
i |
||
m |
|
|
x |
|
$regex with $options syntax$options语法的$regex |
s |
.) to match all characters including newline characters. .)匹配所有字符,包括换行符。.点字符以匹配新行。 |
$regex with $options syntax$options语法的$regex |
Note
The $regex operator does not support the global search modifier g.$regex运算符不支持全局搜索修饰符g。
$regex与/pattern/语法的对比¶$inTo include a regular expression in an 要在$in query expression, you can only use JavaScript regular expression objects (i.e. /pattern/ ). $in查询表达式中包含正则表达式,只能使用JavaScript正则表达式对象(即/pattern/)。For example:例如:
You cannot use 不能在$regex operator expressions inside an $in.$in中使用$regex运算符表达式。
AND Conditions for the FieldAND条件¶To include a regular expression in a comma-separated list of query conditions for the field, use the 要在字段查询条件的逗号分隔列表中包含正则表达式,请使用$regex operator. $regex运算符。For example:例如:
x and s Options¶To use either the 要使用x option or s options, you must use the $regex operator expression with the $options operator. x选项或s选项,必须将$regex运算符表达式与$options运算符一起使用。For example, to specify the 例如,要指定i and the s options, you must use $options for both:i和s选项,必须同时使用$options:
To use PCRE supported features in the regex pattern that are unsupported in JavaScript, you must use the 要在正则表达式模式中使用JavaScript中不支持的PCRE支持的功能,必须使用$regex operator expression with the pattern as a string. $regex运算符表达式,并将该模式作为字符串。For example, to use 例如,要在模式中使用(?i) in the pattern to turn case-insensitivity on for the remaining pattern and (?-i) to turn case-sensitivity on for the remaining pattern, you must use the $regex operator with the pattern as a string:(?i)为剩余模式启用大小写不敏感,以及使用(?-i)为剩余模式启用大小写敏感,必须将$regex运算符作为字符串与模式一起使用:
$regex$not¶Starting in 4.0.7, 从4.0.7开始,$not operator can perform logical NOT operation on both:$not运算符可以对以下两种情况执行逻辑NOT操作:
/pattern/)/pattern/)
For example:例如:
$regexFor example:例如:
In 4.0.6 and earlier, you could use 在4.0.6及更早版本中,可以对正则表达式对象(即$not operator with regular expression objects (i.e. /pattern/) but not with $regex operator expressions./pattern/)使用$not运算符,但不能对$regex运算符表达式使用。
For case sensitive regular expression queries, if an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. 对于区分大小写的正则表达式查询,如果字段存在索引,则MongoDB将正则表达式与索引中的值进行匹配,这可能比集合扫描更快。Further optimization can occur if the regular expression is a “prefix expression”, which means that all potential matches start with the same string. 如果正则表达式是“前缀表达式”,则可能会进行进一步优化,这意味着所有可能的匹配都以相同的字符串开头。This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range.这允许MongoDB根据该前缀构造一个“范围”,并仅与该范围内的索引值匹配。
A regular expression is a “prefix expression” if it starts with a caret (如果正则表达式以插入符号(^) or a left anchor (\A), followed by a string of simple symbols. ^)或左锚(\A)开头,后跟一组简单符号,则它是“前缀表达式”。For example, the regex 例如,regex/^abc.*/ will be optimized by matching only against the values from the index that start with abc./^abc.*/将仅通过匹配以abc开头的索引中的值来优化。
Additionally, while 此外,虽然/^a/, /^a.*/, and /^a.*$/ match equivalent strings, they have different performance characteristics. All of these expressions use an index if an appropriate index exists; however, /^a.*/, and /^a.*$/ are slower. /^a/、/^a.*/和/^a.*$/匹配等效字符串,但它们具有不同的性能特征。如果存在适当的索引,所有这些表达式都使用索引;但是,/^a.*/和/^a.*$/速度较慢。/^a/ can stop scanning after matching the prefix./^a/匹配前缀后可以停止扫描。
Case insensitive regular expression queries generally cannot use indexes effectively. 不区分大小写的正则表达式查询通常不能有效地使用索引。The $regex implementation is not collation-aware and is unable to utilize case-insensitive indexes.$regex实现不支持排序规则,无法使用不区分大小写的索引。
The following examples use a collection 以下示例使用带有以下文档的集合products with the following documents:products:
LIKE MatchLIKE匹配¶The following example matches all documents where the 以下示例匹配sku field is like "%789":sku字段类似于"%789"的所有文档:
The example is analogous to the following SQL LIKE statement:该示例类似于以下类似SQL的语句:
The following example uses the 以下示例使用i option perform a case-insensitive match for documents with sku value that starts with ABC.i选项对sku值以ABC开头的文档执行不区分大小写的匹配。
The query matches the following documents:该查询与以下文档匹配:
The following example uses the 以下示例使用m option to match lines starting with the letter S for multiline strings:m选项匹配多行字符串中以字母S开头的行:
The query matches the following documents:该查询与以下文档匹配:
Without the 如果没有m option, the query would match just the following document:m选项,查询将只匹配以下文档:
If the 如果$regex pattern does not contain an anchor, the pattern matches against the string as a whole, as in the following example:$regex模式不包含锚点,则该模式将与整个字符串匹配,如下例所示:
Then, the 然后,$regex would match both documents:$regex将匹配两个文档:
. Dot Character to Match New Line.点字符以匹配新行¶The following example uses the 以下示例使用s option to allow the dot character (i.e. .) to match all characters including new line as well as the i option to perform a case-insensitive match:s选项允许点字符(即.)要匹配包括新行在内的所有字符,以及执行不区分大小写匹配的i选项,请执行以下操作:
The query matches the following documents:该查询与以下文档匹配:
Without the 如果没有s option, the query would have matched only the following document:s选项,查询将只匹配以下文档:
The following example uses the 下面的示例使用x选项忽略空格和注释,以x option ignore white spaces and the comments, denoted by the # and ending with the \n in the matching pattern:#表示,并在匹配模式中以\n结尾:
The query matches the following document:该查询与以下文档匹配: