On this page本页内容
$regexMatch¶New in version 4.2.版本4.2中的新功能。
Performs a regular expression (regex) pattern matching and returns:执行正则表达式(regex)模式匹配并返回:
true if a match exists.true。false if a match doesn’t exist.false。MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.41 with UTF-8 support.MongoDB使用Perl兼容的正则表达式(即“PCRE”)版本8.41,支持UTF-8。
Prior to MongoDB 4.2, aggregation pipeline can only use the query operator 在MongoDB 4.2之前,聚合管道只能在$regex in the $match stage. $match阶段使用查询运算符$regex。For more information on using regex in a query, see 有关在查询中使用正则表达式的更多信息,请参阅$regex.$regex。
The $regexMatch operator has the following syntax:$regexMatch运算符语法如下所示:
| input |
| ||||||||||
| regex |
| ||||||||||
| options |
Note
|
The operator returns a boolean:运算符返回布尔值:
true if a match exists.true。false if a match doesn’t exist.false。See also参阅
$regexMatch$regexMatch ignores the collation specified for the collection, 忽略为集合db.collection.aggregate(), and the index, if used.db.collection.aggregate()和索引指定的排序规则(如果使用)。
For example, the create a sample collection with collation strength 例如,创建排序规则强度为1 (i.e. compare base character only and ignore other differences such as case and diacritics):1的示例集合(即,仅比较基本字符,忽略其他差异,如大小写和变音符号):
Insert the following documents:插入以下文档:
Using the collection’s collation, the following operation performs a case-insensitive and diacritic-insensitive match:使用集合的排序规则,以下操作执行不区分大小写和不区分重音的匹配:
The operation returns the following 3 documents:该操作返回以下3个文档:
However, the aggregation expression 但是,聚合表达式$regexMatch ignores collation; that is, the following regular expression pattern matching examples are case-sensitive and diacritic sensitive:$regexMatch忽略排序规则;也就是说,以下正则表达式模式匹配示例区分大小写,区分重音:
Both operations return the following:两个操作都返回以下内容:
To perform a case-insensitive regex pattern matching, use the i Option instead. 要执行不区分大小写的regex模式匹配,请改用i选项。See i Option for an example.有关示例,请参阅i选项。
$regexMatchTo illustrate the behavior of the 要演示本示例中讨论的$regexMatch operator as discussed in this example, create a sample collection products with the following documents:$regexMatch运算符的行为,请使用以下文档创建示例集合产品:
By default, 默认情况下,$regexMatch performs a case-sensitive match. $regexMatch执行区分大小写的匹配。For example, the following aggregation performs a case-sensitive 例如,以下聚合在$regexMatch on the description field. description字段上执行区分大小写的$regexMatch。The regex pattern regex模式/line/ does not specify any grouping:/line/未指定任何分组:
The operation returns the following:该操作返回以下内容:
The following regex pattern 以下正则表达式模式/lin(e|k)/ specifies a grouping (e|k) in the pattern:/lin(e|k)/指定模式中的分组(e|k):
The operation returns the following:该操作返回以下内容:
iNote
You cannot specify options in both the 不能同时在regex and the options field.regex和options字段中指定选项。
To perform case-insensitive pattern matching, include the i option as part of the regex field or in the options field:要执行不区分大小写的模式匹配,请在regex字段或options字段中包含i选项:
For example, the following aggregation performs a case-insensitive 例如,以下聚合在$regexMatch on the description field. description字段上执行不区分大小写的$regexMatch。The regex pattern regex模式/line/ does not specify any grouping:/line/未指定任何分组:
The operation returns the following documents:该操作返回以下文档:
mNote
You cannot specify options in both the 不能同时在regex and the options field.regex和options字段中指定选项。
To match the specified anchors (e.g. 要为多行字符串的每一行匹配指定的锚点(例如^, $) for each line of a multiline string, include the m option as part of the regex field or in the options field:^、$),请将m选项作为regex字段的一部分或包含在options字段中:
The following example includes both the 以下示例包括i and the m options to match lines starting with either the letter s or S for multiline strings:i和m选项,用于匹配多行字符串中以字母s或S开头的行:
The operation returns the following:该操作返回以下内容:
xNote
You cannot specify options in both the 不能同时在regex and the options field.regex和options字段中指定选项。
To ignore all unescaped white space characters and comments (denoted by the un-escaped hash 要忽略模式中所有未转义的空白字符和注释(由未转义的哈希# character and the next new-line character) in the pattern, include the s option in the options field:#字符和下一个新行字符表示),请在options字段中包含s选项:
The following example includes the 以下示例包括用于跳过未转义空格和注释的x option to skip unescaped white spaces and comments:x选项:
The operation returns the following:该操作返回以下内容:
sNote
You cannot specify options in both the 不能同时在regex and the options field.regex和options字段中指定选项。
To allow the dot character (i.e. 允许点字符(即.) in the pattern to match all characters including the new line character, include the s option in the options field:.)在要匹配包括新行字符在内的所有字符的模式中,在选项字段中包括s选项:
The following example includes 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 operation returns the following:该操作返回以下内容:
$regexMatch to Check Email Address$regexMatch检查电子邮件地址¶Create a sample collection 使用以下文档创建样本集合feedback with the following documents:feedback:
The following aggregation uses the 以下聚合使用$regexMatch to check if the comment field contains an email address with @mongodb.com and categorize the feedback as Employee or External.$regexMatch检查comment字段是否包含带有@mongodb.com的电子邮件地址,并将反馈分类为员工或外部。
The operation returns the following documents:该操作返回以下文档: