Database Manual / Reference / Query Language / Expressions

$regexMatch (aggregation)(聚合)

Definition定义

$regexMatch

Performs a regular expression (regex) pattern matching and returns:执行正则表达式(regex)模式匹配并返回:

  • true if a match exists.如果匹配存在,则为true
  • false if a match doesn't exist.如果匹配不存在,则为false

Syntax语法

The $regexMatch operator has the following syntax:$regexMatch运算符具有以下语法:

{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }
Field字段Description描述
input

The string on which you wish to apply the regex pattern. 您希望应用正则表达式模式的字符串。Can be a string or any valid expression that resolves to a string.可以是字符串或解析为字符串的任何有效表达式

regex

The regex pattern to apply. Can be any valid expression that resolves to either a string or regex pattern /<pattern>/. 要应用的正则表达式模式。可以是解析为字符串或正则表达式模式/<pattern>/的任何有效表达式When using the regex /<pattern>/, you can also specify the regex options i and m (but not the s or x options):使用正则表达式/<pattern>/时,您还可以指定正则表达式选项im(但不能指定sx选项):

  • "pattern"
  • /<pattern>/
  • /<pattern>/<options>

Alternatively, you can also specify the regex options with the options field. To specify the s or x options, you must use the options field.或者,您还可以使用options字段指定正则表达式选项。要指定sx选项,必须使用options字段。

You cannot specify options in both the regex and the options field.您不能在regexoptions字段中都指定选项。

options

Optional. The following <options> are available for use with regular expression.可选。以下<options>可用于正则表达式。

You cannot specify options in both the regex and the options field.您不能在regexoptions字段中都指定选项。

Option选项Description描述
iCase insensitivity to match both upper and lower cases. You can specify the option in the options field or as part of the regex field.不区分大小写,以匹配大小写。您可以在options字段中指定选项,也可以将其作为正则表达式字段的一部分。
m

For patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. Without this option, these anchors match at beginning or end of the string.对于包含锚点的模式(即^表示开始,$表示结束),在每行的开始或结束处匹配具有多行值的字符串。如果没有此选项,这些锚点将在字符串的开头或结尾匹配。

If the pattern contains no anchors or if the string value has no newline characters (e.g. \n), the m option has no effect.如果模式不包含锚点,或者字符串值没有换行符(例如\n),则m选项无效。

x

"Extended" capability to ignore all white space characters in the pattern unless escaped or included in a character class.“扩展”功能,忽略模式中的所有空白字符,除非转义或包含在字符类中。

Additionally, it ignores characters in-between and including an un-escaped hash/pound (#) character and the next new line, so that you may include comments in complicated patterns. This only applies to data characters; white space characters may never appear within special character sequences in a pattern.此外,它忽略了中间的字符,包括一个未转义的哈希/磅(#)字符和下一个新行,这样你就可以在复杂的模式中包含注释。这仅适用于数据字符;空白字符可能永远不会出现在模式中的特殊字符序列中。

The x option does not affect the handling of the VT character (i.e. code 11).x选项不影响VT字符的处理(即代码11)。

You can specify the option only in the options field.您只能在options字段中指定选项。

s

Allows the dot character (i.e. .) to match all characters including newline characters.允许点字符(即.)匹配所有字符,包括换行符。

You can specify the option only in the options field.您只能在options字段中指定选项。

Returns

The operator returns a boolean:运算符返回一个布尔值:

  • true if a match exists.如果匹配存在,则为true
  • false if a match doesn't exist.如果匹配不存在,则为false

Behavior行为

PCRE Library

Starting in version 6.1, MongoDB uses the PCRE2 (Perl Compatible Regular Expressions) library to implement regular expression pattern matching. 从6.1版本开始,MongoDB使用PCRE2(Perl兼容正则表达式)库来实现正则表达式模式匹配。To learn more about PCRE2, see the PCRE Documentation.要了解有关PCRE2的更多信息,请参阅PCRE文档

$regexMatch and Collation$regexMatch和排序规则

String matching for $regexMatch is always case-sensitive and diacritic-sensitive. $regexMatch ignores the collation specified for the collection, db.collection.aggregate(), and the index, if used.$regexMatch的字符串匹配始终区分大小写和变音符号。$regexMatch忽略为集合指定的排序规则db.collection.aggregate()和索引(如果使用)。

For example, create a collection with collation strength 1, meaning the collation only compares base characters and ignores differences such as case and diacritics:例如,创建排序规则强度为1的集合,这意味着排序规则只比较基本字符,忽略大小写和变音符号等差异:

db.createCollection( "restaurants", { collation: { locale: "fr", strength: 1 } } )

Insert the following documents:插入以下文件:

db.restaurants.insertMany( [
{ _id: 1, category: "café", status: "Open" },
{ _id: 2, category: "cafe", status: "open" },
{ _id: 3, category: "cafE", status: "open" }
] )

The following uses the collection's collation to perform a case-insensitive and diacritic-insensitive match:以下内容使用集合的排序规则来执行不区分大小写和不区分变音符号的匹配:

db.restaurants.aggregate( [ { $match: { category: "cafe" } } ] )
[
{ _id: 1, category: 'café', status: 'Open' },
{ _id: 2, category: 'cafe', status: 'open' },
{ _id: 3, category: 'cafE', status: 'open' }
]

However, $regexMatch ignores collation. The following regular expression pattern matching examples are case-sensitive and diacritic sensitive:但是,$regexMatch忽略排序规则。以下正则表达式模式匹配示例区分大小写和变音符号:

db.restaurants.aggregate( [
{
$addFields: {
resultObject: { $regexMatch: { input: "$category", regex: /cafe/ } }
}
}
] )

db.restaurants.aggregate( [
{
$addFields: {
resultObject: { $regexMatch: { input: "$category", regex: /cafe/ } }
}
}
],
{ collation: { locale: "fr", strength: 1 } } // Ignored in the $regexMatch
)

Both operations return the following:这两个操作都返回以下内容:

{ "_id" : 1, "category" : "café", "resultObject" : null }
{ "_id" : 2, "category" : "cafe", "resultObject" : { "match" : "cafe", "idx" : 0, "captures" : [ ] } }
{ "_id" : 3, "category" : "cafE", "resultObject" : null }

Because the query ignores the collation, it requires an exact match on the category string (including case and accent marks), meaning only document _id: 2 is matched.因为查询忽略了排序规则,所以它要求category字符串(包括大小写和重音标记)完全匹配,这意味着只匹配文档_id:2

To perform a case-insensitive regex pattern matching, use the i Option instead. See i Option for an example.要执行不区分大小写的正则表达式模式匹配,请改用i选项。示例见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执行区分大小写的匹配。例如,以下聚合对描述字段执行区分大小写的$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 /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选项

Note

You cannot specify options in both the regex and the options field.您不能在regexoptions字段中都指定选项。

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将i指定为正则表达式字段的一部分
{ $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. The regex pattern /line/ does not specify any grouping:例如,以下聚合对description字段执行不区分大小写的$regexMatch。正则表达式模式/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选项

Note

You cannot specify options in both the regex and the options field.您不能在regexoptions字段中都指定选项。

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:以下示例包括im选项,用于匹配多行字符串中以字母sS开头的行:

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选项

Note

You cannot specify options in both the regex and the options field.您不能在regexoptions字段中都指定选项。

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 Option选项

Note

You cannot specify options in both the regex and the options field.您不能在regexoptions字段中都指定选项。

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在选项字段中指定s
{ $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检查电子邮件地址

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的电子邮件地址,并将反馈分类为EmployeeExternal

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" }