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 在MongoDB4.2之前,聚合管道只能在$regex
in the $match
stage. $match
阶段使用查询运算符$regex
。For more information on using regex in a query, see 有关在查询中使用$regex
.$regex
的详细信息,请参阅$regex
。
The $regexMatch
operator has the following syntax:$regexMatch
运算符语法如下:
{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }
input |
| ||||||||||
regex |
| ||||||||||
options |
|
The operator returns a boolean:运算符返回布尔值:
true
if a match exists.true
。false
if a match doesn't exist.false
。$regexMatch
$regexMatch
ignores the collation specified for the collection, db.collection.aggregate()
, and the index, if used.$regexMatch
忽略为集合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 要执行不区分大小写的正则表达式模式匹配,请改用i
Option instead. i
选项。See 有关示例,请参阅i
Option for an example.i
选项。
$regexMatch
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. $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/
未指定任何分组:
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 以下正则表达式模式/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
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
选项:
// 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 regex模式/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
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
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
选项:
// 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
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
字段中包含options选项:
// 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 }
$regexMatch
to Check Email Address$regexMatch
检查电子邮件地址Create 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" }