$regexFindAll (aggregation)
On this page本页内容
Definition定义
$regexFindAll
New in version 4.2.4.2版新增。Provides regular expression (regex) pattern matching capability in aggregation expressions. The operator returns an array of documents that contains information on each match. If a match is not found, returns an empty array.在聚合表达式中提供正则表达式(regex)模式匹配功能。运算符返回一个文档数组,其中包含每个匹配项的信息。如果未找到匹配项,则返回一个空数组。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
.$regex
。
Syntax语法
The $regexFindAll
operator has the following syntax:$regexFindAll
运算符具有以下语法:
{ $regexFindAll: { 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>/ 时,还可以指定regex选项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 an array:运算符返回一个数组:
If the operator does not find a match, the operator returns an empty array.如果运算符找不到匹配项,则运算符将返回一个空数组。If the operator finds a match, the operator returns an array of documents that contains the following information for each match:如果运算符找到匹配项,则运算符会返回一组文档,其中包含每个匹配项的以下信息:the matching string in the input,input
中的匹配字符串,the code point输入中匹配字符串的代码点index (not byte index) of the matching string in the input, and
索引(而非字节索引),以及
An array of the strings that corresponds to the groups captured by the matching string.与匹配字符串捕获的组相对应的字符串数组。Capturing groups are specified with unescaped parenthesis捕获组是用()
in the regex pattern.regex
模式中未加括号()
指定的。
[ { "match" : <string>, "idx" : <num>, "captures" : <array of strings> }, ... ]
See also: 另请参阅:
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(PerlCompatibleRegularExpressions)库来实现正则表达式模式匹配。To learn more about PCRE2, see the PCRE Documentation.要了解有关PCRE2的更多信息,请参阅PCRE文档。
$regexFindAll
and Collation和排序规则
$regexFindAll
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 但是,聚合表达式$regexFind
ignores collation; that is, the following regular expression pattern matching examples are case-sensitive and diacritic sensitive:$regexFind
忽略排序规则;也就是说,以下正则表达式模式匹配示例区分大小写和变音符号:
db.myColl.aggregate( [ { $addFields: { results: { $regexFindAll: { input: "$category", regex: /cafe/ } } } } ] )
db.myColl.aggregate(
[ { $addFields: { results: { $regexFindAll: { input: "$category", regex: /cafe/ } } } } ],
{ collation: { locale: "fr", strength: 1 } } // Ignored in the $regexFindAll
)
Both operations return the following:两个操作都返回以下内容:
{ "_id" : 1, "category" : "café", "results" : [ ] }
{ "_id" : 2, "category" : "cafe", "results" : [ { "match" : "cafe", "idx" : 0, "captures" : [ ] } ] }
{ "_id" : 3, "category" : "cafE", "results" : [ ] }
To perform a case-insensitive regex pattern matching, use the 要执行不区分大小写的regex模式匹配,请改用i
Option instead. See i
Option for an example.i
选项。有关示例,请参阅i
选项。
captures
Output Behavior输出行为
If your regex pattern contains capture groups and the pattern finds a match in the input, the 如果captures
array in the results corresponds to the groups captured by the matching string. regex
模式包含捕获组,并且该模式在input
中找到匹配项,则结果中的捕获数组与匹配字符串捕获的组相对应。Capture groups are specified with unescaped parentheses 捕获组是用()
in the regex pattern. regex
模式中未加括号的括号()
指定的。The length of the captures
array equals the number of capture groups in the pattern and the order of the array matches the order in which the capture groups appear.captures
数组的长度等于模式中捕获组的数量,并且数组的顺序与捕获组的出现顺序相匹配。
Create a sample collection named 使用以下文档创建名为contacts
with the following documents:contacts
的示例集合:
db.contacts.insertMany([
{ "_id": 1, "fname": "Carol", "lname": "Smith", "phone": "718-555-0113" },
{ "_id": 2, "fname": "Daryl", "lname": "Doe", "phone": "212-555-8832" },
{ "_id": 3, "fname": "Polly", "lname": "Andrews", "phone": "208-555-1932" },
{ "_id": 4, "fname": "Colleen", "lname": "Duncan", "phone": "775-555-0187" },
{ "_id": 5, "fname": "Luna", "lname": "Clarke", "phone": "917-555-4414" }
])
The following pipeline applies the regex pattern 以下管道将/(C(ar)*)ol/
to the fname
field:regex
模式/(C(ar)*)ol/
应用于fname
字段:
db.contacts.aggregate([
{
$project: {
returnObject: {
$regexFindAll: { input: "$fname", regex: /(C(ar)*)ol/ }
}
}
}
])
The regex pattern finds a match with fname
values Carol
and Colleen
:regex
模式查找与fname
值Carol
和Colleen
匹配的值:
{ "_id" : 1, "returnObject" : [ { "match" : "Carol", "idx" : 0, "captures" : [ "Car", "ar" ] } ] }
{ "_id" : 2, "returnObject" : [ ] }
{ "_id" : 3, "returnObject" : [ ] }
{ "_id" : 4, "returnObject" : [ { "match" : "Col", "idx" : 0, "captures" : [ "C", null ] } ] }
{ "_id" : 5, "returnObject" : [ ] }
The pattern contains the capture group 该模式包含捕获组(C(ar)*)
which contains the nested group (ar)
. (C(ar)*)
,该捕获组包含嵌套组(ar)
。The elements in the captures
array correspond to the two capture groups. captures
数组中的元素对应于两个捕获组。If a matching document is not captured by a group (e.g. 如果一个组(例如Colleen
and the group (ar)
), $regexFindAll
replaces the group with a null placeholder.Colleen
和组(ar)
)没有捕获匹配的文档,$regexFindAll
将用null
占位符替换该组。
As shown in the previous example, the 如前一个示例所示,captures
array contains an element for each capture group (using null
for non-captures). captures
数组包含每个捕获组的一个元素(对非捕获使用null
)。Consider the following example which searches for phone numbers with New York City area codes by applying a logical 考虑以下示例,该示例通过将逻辑or
of capture groups to the phone
field. or
捕获组应用于phone
字段来搜索具有纽约市区号的电话号码。Each group represents a New York City area code:每组代表一个纽约市区号:
db.contacts.aggregate([
{
$project: {
nycContacts: {
$regexFindAll: { input: "$phone", regex: /^(718).*|^(212).*|^(917).*/ }
}
}
}
])
For documents which are matched by the regex pattern, the 对于captures
array includes the matching capture group and replaces any non-capturing groups with null
:regex
模式匹配的文档,captures
数组包括匹配的捕获组,并将任何非捕获组替换为null
:
{ "_id" : 1, "nycContacts" : [ { "match" : "718-555-0113", "idx" : 0, "captures" : [ "718", null, null ] } ] }
{ "_id" : 2, "nycContacts" : [ { "match" : "212-555-8832", "idx" : 0, "captures" : [ null, "212", null ] } ] }
{ "_id" : 3, "nycContacts" : [ ] }
{ "_id" : 4, "nycContacts" : [ ] }
{ "_id" : 5, "nycContacts" : [ { "match" : "917-555-4414", "idx" : 0, "captures" : [ null, null, "917" ] } ] }
Examples实例
$regexFindAll
and Its Options及其选项
To illustrate the behavior of the 为了说明本例中讨论的$regexFindAll
operator as discussed in this example, create a sample collection products
with the following documents:$regexFindAll
运算符的行为,请使用以下文档创建一个示例集合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, 默认情况下,$regexFindAll
performs a case-sensitive match. $regexFindAll
执行区分大小写的匹配。For example, the following aggregation performs a case-sensitive 例如,以下聚合对$regexFindAll
on the description
field. description
字段执行区分大小写的$regexFindAll
。The regex pattern 正则表达式模式/line/
does not specify any grouping:/line/
未指定任何分组:
db.products.aggregate([
{ $addFields: { returnObject: { $regexFindAll: { input: "$description", regex: /line/ } } } }
])
The operation returns the following:该操作返回以下内容:
{
"_id" : 1,
"description" : "Single LINE description.",
"returnObject" : [ ]
}
{
"_id" : 2,
"description" : "First lines\nsecond line",
"returnObject" : [ { "match" : "line", "idx" : 6, "captures" : [ ]}, { "match" : "line", "idx" : 19, "captures" : [ ] } ]
}
{
"_id" : 3,
"description" : "Many spaces before line",
"returnObject" : [ { "match" : "line", "idx" : 23, "captures" : [ ] } ]
}
{
"_id" : 4,
"description" : "Multiple\nline descriptions",
"returnObject" : [ { "match" : "line", "idx" : 9, "captures" : [ ] }
] }
{
"_id" : 5,
"description" : "anchors, links and hyperlinks",
"returnObject" : [ ]
}
{
"_id" : 6,
"description" : "métier work vocation",
"returnObject" : [ ]
}
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: { returnObject: { $regexFindAll: { input: "$description", regex: /lin(e|k)/ } } } }
])
The operation returns the following:该操作返回以下内容:
{
"_id" : 1,
"description" : "Single LINE description.",
"returnObject": [ ]
}
{
"_id" : 2,
"description" : "First lines\nsecond line",
"returnObject" : [ { "match" : "line", "idx" : 6, "captures" : [ "e" ] }, { "match" : "line", "idx" : 19, "captures" : [ "e" ] } ]
}
{
"_id" : 3,
"description" : "Many spaces before line",
"returnObject" : [ { "match" : "line", "idx" : 23, "captures" : [ "e" ] } ]
}
{
"_id" : 4,
"description" : "Multiple\nline descriptions",
"returnObject" : [ { "match" : "line", "idx" : 9, "captures" : [ "e" ] } ]
}
{
"_id" : 5,
"description" : "anchors, links and hyperlinks",
"returnObject" : [ { "match" : "link", "idx" : 9, "captures" : [ "k" ] }, { "match" : "link", "idx" : 24, "captures" : [ "k" ] } ]
}
{
"_id" : 6,
"description" : "métier work vocation",
"returnObject" : [ ]
}
In the return option, the 在idx
field is the code point index and not the byte index.
return
选项中,idx
字段是代码点索引,而不是字节索引。
To illustrate, consider the following example that uses the regex pattern 为了进行说明,请考虑以下使用正则表达式模式/tier/
:/tier/
的示例:
db.products.aggregate([
{ $addFields: { returnObject: { $regexFindAll: { input: "$description", regex: /tier/ } } } }
])
The operation returns the following where only the last record matches the pattern and the returned 该操作返回以下内容,其中只有最后一条记录与模式匹配,并且返回的idx
is 2
(instead of 3 if using a byte index)idx
为2
(如果使用字节索引,则为3)
{ "_id" : 1, "description" : "Single LINE description.", "returnObject" : [ ] }
{ "_id" : 2, "description" : "First lines\nsecond line", "returnObject" : [ ] }
{ "_id" : 3, "description" : "Many spaces before line", "returnObject" : [ ] }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returnObject" : [ ] }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returnObject" : [ ] }
{ "_id" : 6, "description" : "métier work vocation",
"returnObject" : [ { "match" : "tier", "idx" : 2, "captures" : [ ] } ] }
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
{ $regexFindAll: { input: "$description", regex: /line/i } }
// Specify i in the options field
{ $regexFindAll: { input: "$description", regex: /line/, options: "i" } }
{ $regexFindAll: { input: "$description", regex: "line", options: "i" } }
For example, the following aggregation performs a case-insensitive 例如,以下聚合对描述字段执行不区分大小写的$regexFindAll
on the description
field. $regexFindAll
。The regex pattern 正则表达式模式/line/
does not specify any grouping:/line/
未指定任何分组:
db.products.aggregate([
{ $addFields: { returnObject: { $regexFindAll: { input: "$description", regex: /line/i } } } }
])
The operation returns the following documents:该操作返回以下文档:
{
"_id" : 1,
"description" : "Single LINE description.",
"returnObject" : [ { "match" : "LINE", "idx" : 7, "captures" : [ ] } ]
}
{
"_id" : 2,
"description" : "First lines\nsecond line",
"returnObject" : [ { "match" : "line", "idx" : 6, "captures" : [ ] }, { "match" : "line", "idx" : 19, "captures" : [ ] } ]
}
{
"_id" : 3,
"description" : "Many spaces before line",
"returnObject" : [ { "match" : "line", "idx" : 23, "captures" : [ ] } ]
}
{
"_id" : 4,
"description" : "Multiple\nline descriptions",
"returnObject" : [ { "match" : "line", "idx" : 9, "captures" : [ ] } ]
}
{
"_id" : 5,
"description" : "anchors, links and hyperlinks",
"returnObject" : [ ]
}
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : [ ] }
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
{ $regexFindAll: { input: "$description", regex: /line/m } }
// Specify m in the options field
{ $regexFindAll: { input: "$description", regex: /line/, options: "m" } }
{ $regexFindAll: { 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: { returnObject: { $regexFindAll: { input: "$description", regex: /^s/im } } } }
])
The operation returns the following:该操作返回以下内容:
{
"_id" : 1,
"description" : "Single LINE description.",
"returnObject" : [ { "match" : "S", "idx" : 0, "captures" : [ ] } ]
}
{
"_id" : 2,
"description" : "First lines\nsecond line",
"returnObject" : [ { "match" : "s", "idx" : 12, "captures" : [ ] } ]
}
{
"_id" : 3,
"description" : "Many spaces before line",
"returnObject" : [ ]
}
{
"_id" : 4,
"description" : "Multiple\nline descriptions",
"returnObject" : [ ]
}
{
"_id" : 5,
"description" : "anchors, links and hyperlinks",
"returnObject" : [ ]
}
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : [ ] }
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
{ $regexFindAll: { input: "$description", regex: /line/, options: "x" } }
{ $regexFindAll: { 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: { returnObject: { $regexFindAll: { input: "$description", regex: /lin(e|k) # matches line or link/, options:"x" } } } }
])
The operation returns the following:该操作返回以下内容:
{
"_id" : 1,
"description" : "Single LINE description.",
"returnObject" : [ ]
}
{
"_id" : 2,
"description" : "First lines\nsecond line",
"returnObject" : [ { "match" : "line", "idx" : 6, "captures" : [ "e" ] }, { "match" : "line", "idx" : 19, "captures" : [ "e" ] } ]
}
{
"_id" : 3,
"description" : "Many spaces before line",
"returnObject" : [ { "match" : "line", "idx" : 23, "captures" : [ "e" ] } ]
}
{
"_id" : 4,
"description" : "Multiple\nline descriptions",
"returnObject" : [ { "match" : "line", "idx" : 9, "captures" : [ "e" ] } ]
}
{
"_id" : 5,
"description" : "anchors, links and hyperlinks",
"returnObject" : [ { "match" : "link", "idx" : 9, "captures" : [ "k" ] }, { "match" : "link", "idx" : 24, "captures" : [ "k" ] } ]
}
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : [ ] }
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
{ $regexFindAll: { input: "$description", regex: /m.*line/, options: "s" } }
{ $regexFindAll: { 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: { returnObject: { $regexFindAll: { input: "$description", regex:/m.*line/, options: "si" } } } }
])
The operation returns the following:该操作返回以下内容:
{
"_id" : 1,
"description" : "Single LINE description.",
"returnObject" : [ ]
}
{
"_id" : 2,
"description" : "First lines\nsecond line",
"returnObject" : [ ]
}
{
"_id" : 3,
"description" : "Many spaces before line",
"returnObject" : [ { "match" : "Many spaces before line", "idx" : 0, "captures" : [ ] } ]
}
{
"_id" : 4,
"description" : "Multiple\nline descriptions",
"returnObject" : [ { "match" : "Multiple\nline", "idx" : 0, "captures" : [ ] } ]
}
{
"_id" : 5,
"description" : "anchors, links and hyperlinks",
"returnObject" : [ ]
}
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : [ ] }
Use $regexFindAll
to Parse Email from String使用$regexFindAll
从字符串分析电子邮件
$regexFindAll
to Parse Email from StringCreate 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 以下聚合使用$regexFindAll
to extract all emails from the comment
field (case insensitive).$regexFindAll
从comment
字段中提取所有电子邮件(不区分大小写)。
db.feedback.aggregate( [
{ $addFields: {
"email": { $regexFindAll: { input: "$comment", regex: /[a-z0-9_.+-]+@[a-z0-9_.+-]+\.[a-z0-9_.+-]+/i } }
} },
{ $set: { email: "$email.match"} }
] )
First Stage第一阶段-
The stage uses the该阶段使用$addFields
stage to add a new fieldemail
to the document.$addFields
阶段将新的字段电子邮件添加到文档中。The new field is an array that contains the result of performing the新字段是一个数组,包含对$regexFindAll
on thecomment
field:comment
字段执行$regexFindAll
的结果:{ "_id" : 1, "comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com", "email" : [ { "match" : "aunt.arc.tica@example.com", "idx" : 38, "captures" : [ ] } ] }
{ "_id" : 2, "comment" : "I wanted to concatenate a string", "email" : [ ] }
{ "_id" : 3, "comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com", "email" : [ { "match" : "cam@mongodb.com", "idx" : 56, "captures" : [ ] }, { "match" : "c.dia@mongodb.com", "idx" : 75, "captures" : [ ] } ] }
{ "_id" : 4, "comment" : "It's just me. I'm testing. fred@MongoDB.com", "email" : [ { "match" : "fred@MongoDB.com", "idx" : 28, "captures" : [ ] } ] } Second Stage第二阶段-
The stage use the该阶段使用$set
stage to reset theemail
array elements to the"email.match"
value(s).$set
阶段将email
数组元素重置为"email.match"
值。If the current value of如果email
is null, the new value ofemail
is set to null.email
的当前值为null
,则将email
的新值设置为null
。{ "_id" : 1, "comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com", "email" : [ "aunt.arc.tica@example.com" ] }
{ "_id" : 2, "comment" : "I wanted to concatenate a string", "email" : [ ] }
{ "_id" : 3, "comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com", "email" : [ "cam@mongodb.com", "c.dia@mongodb.com" ] }
{ "_id" : 4, "comment" : "It's just me. I'm testing. fred@MongoDB.com", "email" : [ "fred@MongoDB.com" ] }
Use Captured Groupings to Parse User Name使用捕获的分组分析用户名
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" }
])
To reply to the feedback, assume you want to parse the local-part of the email address to use as the name in the greetings. 要回复反馈,假设您想解析电子邮件地址的本地部分,用作问候语中的名称。Using the 使用captured
field returned in the $regexFindAll
results, you can parse out the local part of each email address:$regexFindAll
结果中返回的captured
字段,您可以解析出每个电子邮件地址的本地部分:
db.feedback.aggregate( [
{ $addFields: {
"names": { $regexFindAll: { input: "$comment", regex: /([a-z0-9_.+-]+)@[a-z0-9_.+-]+\.[a-z0-9_.+-]+/i } },
} },
{ $set: { names: { $reduce: { input: "$names.captures", initialValue: [ ], in: { $concatArrays: [ "$$value", "$$this" ] } } } } }
] )
First Stage第一阶段-
The stage uses the该阶段使用$addFields
stage to add a new fieldnames
to the document.$addFields
阶段向文档添加新的字段names
。The new field contains the result of performing the新字段包含对$regexFindAll
on thecomment
field:comment
字段执行$regexFindAll
的结果:{
"_id" : 1,
"comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com",
"names" : [ { "match" : "aunt.arc.tica@example.com", "idx" : 38, "captures" : [ "aunt.arc.tica" ] } ]
}
{ "_id" : 2, "comment" : "I wanted to concatenate a string", "names" : [ ] }
{
"_id" : 3,
"comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com",
"names" : [
{ "match" : "cam@mongodb.com", "idx" : 56, "captures" : [ "cam" ] },
{ "match" : "c.dia@mongodb.com", "idx" : 75, "captures" : [ "c.dia" ] }
]
}
{
"_id" : 4,
"comment" : "It's just me. I'm testing. fred@MongoDB.com",
"names" : [ { "match" : "fred@MongoDB.com", "idx" : 28, "captures" : [ "fred" ] } ]
} Second Stage第二阶段-
The stage use the该阶段使用带有$set
stage with the$reduce
operator to resetnames
to an array that contains the"$names.captures"
elements.$reduce
运算符的$set
阶段将names
重置为包含"$names.captures"
元素的数组。{
"_id" : 1,
"comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com",
"names" : [ "aunt.arc.tica" ]
}
{ "_id" : 2, "comment" : "I wanted to concatenate a string", "names" : [ ] }
{
"_id" : 3,
"comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com",
"names" : [ "cam", "c.dia" ]
}
{
"_id" : 4,
"comment" : "It's just me. I'm testing. fred@MongoDB.com",
"names" : [ "fred" ]
}
See also: 另请参阅:
For more information on the behavior of the 有关captures
array and additional examples, see captures
Output Behavior.captures
数组行为的更多信息和其他示例,请参阅captures
输出行为。