$regexMatch (aggregation)
On this page本页内容
Definition定义
$regexMatchNew in version 4.2.4.2版新增。Performs a regular expression (regex) pattern matching and returns:执行正则表达式(regex)模式匹配并返回:如果存在匹配,则为trueif a match exists.true。如果不存在匹配项,则为falseif a match doesn't exist.false。
Prior to MongoDB 4.2, aggregation pipeline can only use the query operator在MongoDB 4.2之前,聚合管道只能在$regexin the$matchstage.$match阶段使用查询运算符$regex。For more information on using regex in a query, see有关在查询中使用regex的更多信息,请参阅$regex.$regex。
Syntax语法
The $regexMatch operator has the following syntax:$regexMatch运算符具有以下语法:
{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }
input | |||||||||||
regex | /<pattern>/. /<pattern>/的任何有效表达式。/<pattern>/, you can also specify the regex options i and m (but not the s or x options): /<pattern>/时,还可以指定正则表达式选项i和m(但不能指定s或x选项):
options字段指定regex选项。s or x options, you must use the options field.s或x选项,必须使用options字段。regex and the options field. regex和options字段中都指定选项。 | ||||||||||
options | <options> are available for use with regular expression. <options>可用于正则表达式。Note regex and the options field. regex和options字段中都指定选项。
|
Returns返回值
The operator returns a boolean:运算符返回布尔值:
如果存在匹配,则为trueif a match exists.true。如果不存在匹配项,则为falseif a match doesn't exist.false。
See also: 另请参阅:
Behavior行为
PCRE LibraryPCRE库
Starting in version 6.1, MongoDB uses the PCRE2 (Perl Compatible Regular Expressions) library to implement regular expression pattern matching. 从6.1版本开始,MongoDB使用PCRE2(PerlCompatibleRegularExpressions)库来实现正则表达式模式匹配。To learn more about PCRE2, see the PCRE Documentation.要了解有关PCRE2的更多信息,请参阅PCRE文档。
$regexMatch and Collation和排序规则
$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的示例集合(即,仅比较基本字符,忽略其他差异,如大小写和变音符号):
db.createCollection( "myColl", { collation: { locale: "fr", strength: 1 } } )
Insert the following documents:插入以下文件:
db.myColl.insertMany([
{ _id: 1, category: "café" },
{ _id: 2, category: "cafe" },
{ _id: 3, category: "cafE" }
])
Using the collection's collation, the following operation performs a case-insensitive and diacritic-insensitive match:使用集合的排序规则,以下操作执行不区分大小写和不区分重音符号的匹配:
db.myColl.aggregate( [ { $match: { category: "cafe" } } ] )
The operation returns the following 3 documents:该操作返回以下3个文档:
{ "_id" : 1, "category" : "café" }
{ "_id" : 2, "category" : "cafe" }
{ "_id" : 3, "category" : "cafE" }
However, the aggregation expression 但是,聚合表达式$regexMatch ignores collation; that is, the following regular expression pattern matching examples are case-sensitive and diacritic sensitive:$regexMatch忽略排序规则;也就是说,以下正则表达式模式匹配示例区分大小写和变音符号:
db.myColl.aggregate( [ { $addFields: { results: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ] )
db.myColl.aggregate(
[ { $addFields: { results: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ],
{ collation: { locale: "fr", strength: 1 } } // Ignored in the $regexMatch
)
Both operations return the following:两个操作都返回以下内容:
{ "_id" : 1, "category" : "café", "results" : false }
{ "_id" : 2, "category" : "cafe", "results" : true }
{ "_id" : 3, "category" : "cafE", "results" : false }
To perform a case-insensitive regex pattern matching, use the 要执行不区分大小写的regex模式匹配,请改用i Option instead. i选项。See 有关示例,请参阅i Option for an example.i选项。
Examples实例
$regexMatch and Its Options及其选项
To illustrate the behavior of the 为了说明本例中讨论的$regexMatch operator as discussed in this example, create a sample collection products with the following documents:$regexMatch运算符的行为,请使用以下文档创建一个示例集合products:
db.products.insertMany([
{ _id: 1, description: "Single LINE description." },
{ _id: 2, description: "First lines\nsecond line" },
{ _id: 3, description: "Many spaces before line" },
{ _id: 4, description: "Multiple\nline descriptions" },
{ _id: 5, description: "anchors, links and hyperlinks" },
{ _id: 6, description: "métier work vocation" }
])
By default, 默认情况下,$regexMatch performs a case-sensitive match. For example, the following aggregation performs a case-sensitive $regexMatch on the description field. The regex pattern /line/ does not specify any grouping:$regexMatch执行区分大小写的匹配。例如,以下聚合对description字段执行区分大小写的$regexMatch。正则表达式模式/line/未指定任何分组:
db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /line/ } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "result" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }
The following regex pattern 以下regex模式/lin(e|k)/ specifies a grouping (e|k) in the pattern:/lin(e|k)/指定模式中的分组(e|k):
db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /lin(e|k)/ } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "result" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : true }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }
i Option选项
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:要执行不区分大小写的模式匹配,请将i选项作为regex字段或options字段的一部分:
// Specify i as part of the regex field
{ $regexMatch: { input: "$description", regex: /line/i } }
// Specify i in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "i" } }
{ $regexMatch: { input: "$description", regex: "line", options: "i" } }
For example, the following aggregation performs a case-insensitive 例如,以下聚合对$regexMatch on the description field. description字段执行不区分大小写的$regexMatch。The regex pattern 正则表达式模式/line/ does not specify any grouping:/line/未指定任何分组:
db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /line/i } } } }
])
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 1, "description" : "Single LINE description.", "result" : true }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }
m Option
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字段的一部分:
// Specify m as part of the regex field
{ $regexMatch: { input: "$description", regex: /line/m } }
// Specify m in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "m" } }
{ $regexMatch: { input: "$description", regex: "line", options: "m" } }
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开头的行:
db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /^s/im } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "result" : true }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : false }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : false }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }
x Option选项
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 要忽略模式中所有未转义的空白字符和注释(由未转义的hash# character and the next new-line character) in the pattern, include the s option in the options field:#字符和下一个换行符表示),请在options字段中包含s选项:
// Specify x in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "x" } }
{ $regexMatch: { input: "$description", regex: "line", options: "x" } }
The following example includes the 以下示例包括用于跳过未跳过的空白和注释的x option to skip unescaped white spaces and comments:x选项:
db.products.aggregate([
{ $addFields: { returns: { $regexMatch: { input: "$description", regex: /lin(e|k) # matches line or link/, options:"x" } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "returns" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "returns" : true }
{ "_id" : 3, "description" : "Many spaces before line", "returns" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returns" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returns" : true }
{ "_id" : 6, "description" : "métier work vocation", "returns" : false }
s Option
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:.)与包括换行符在内的所有字符匹配,请在options字段中包含s选项:
// Specify s in the options field
{ $regexMatch: { input: "$description", regex: /m.*line/, options: "s" } }
{ $regexMatch: { input: "$description", regex: "m.*line", options: "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选项:
db.products.aggregate([
{ $addFields: { returns: { $regexMatch: { input: "$description", regex:/m.*line/, options: "si" } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "returns" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "returns" : false }
{ "_id" : 3, "description" : "Many spaces before line", "returns" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returns" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returns" : false }
{ "_id" : 6, "description" : "métier work vocation", "returns" : false }
Use $regexMatch to Check Email Address使用$regexMatch检查电子邮件地址
$regexMatch to Check Email AddressCreate a sample collection 使用以下文档创建示例集合feedback with the following documents:feedback:
db.feedback.insertMany([
{ "_id" : 1, comment: "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com" },
{ "_id" : 2, comment: "I wanted to concatenate a string" },
{ "_id" : 3, comment: "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com" },
{ "_id" : 4, comment: "It's just me. I'm testing. fred@MongoDB.com" }
])
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的电子邮件地址,并将反馈分类为Employee或External。
db.feedback.aggregate( [
{ $addFields: {
"category": { $cond: { if: { $regexMatch: { input: "$comment", regex: /[a-z0-9_.+-]+@mongodb.com/i } },
then: "Employee",
else: "External" } }
} },
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 1, "comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com", "category" : "External" }
{ "_id" : 2, "comment" : "I wanted to concatenate a string", "category" : "External" }
{ "_id" : 3, "comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com", "category" : "Employee" }
{ "_id" : 4, "comment" : "It's just me. I'm testing. fred@MongoDB.com", "category" : "Employee" }