Definition定义
$regexFindProvides regular expression (regex) pattern matching capability in aggregation expressions. If a match is found, returns a document that contains information on the first match. If a match is not found, returns null.在聚合表达式中提供正则表达式(regex)模式匹配功能。如果找到匹配项,则返回一个包含第一个匹配项信息的文档。如果未找到匹配项,则返回null。
Syntax语法
The $regexFind operator has the following syntax:$regexFind运算符具有以下语法:
{ $regexFind: { input: <expression> , regex: <expression>, options: <expression> } }
Operator Fields运算符字段
input |
| ||||||||||
regex |
| ||||||||||
options |
|
Returns值
If the operator does not find a match, the result of the operator is a 如果运算符没有找到匹配项,则运算符的结果为null.null。
If the operator finds a match, the result of the operator is a document that contains:如果运算符找到匹配项,则运算符的结果是一个包含以下内容的文档:
the first matching string in the input,input中的第一个匹配字符串,the code point index (not byte index) of the matching string in the input, andinput中匹配字符串的码点索引(不是字节索引),以及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> }
Behavior行为
PCRE LibraryPCRE库
Starting in version 6.1, MongoDB uses the PCRE2 (Perl Compatible Regular Expressions) library to implement regular expression pattern matching. To learn more about PCRE2, see the PCRE Documentation.从6.1版本开始,MongoDB使用PCRE2(Perl兼容正则表达式)库来实现正则表达式模式匹配。要了解有关PCRE2的更多信息,请参阅PCRE文档。
$regexFind and Collation$regex查找和排序规则
String matching for $regexFind is always case-sensitive and diacritic-sensitive. $regexFind ignores the collation specified for the collection, db.collection.aggregate(), and the index, if used.$regexFind的字符串匹配始终区分大小写和变音符号。$regexFind忽略为集合指定的排序规则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, 但是,$regexFind ignores collation. The following regular expression pattern matching examples are case-sensitive and diacritic sensitive:$regexFind忽略排序规则。以下正则表达式模式匹配示例区分大小写和变音符号:
db.restaurants.aggregate( [
{
$addFields: {
resultObject: { $regexFind: { input: "$category", regex: /cafe/ } }
}
}
] )
db.restaurants.aggregate( [
{
$addFields: {
resultObject: { $regexFind: { input: "$category", regex: /cafe/ } }
}
}
],
{ collation: { locale: "fr", strength: 1 } } // Ignored in the $regexFind
)
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字符串(包括大小写和重音标记)完全匹配,这意味着只匹配document_id:2。
To perform a case-insensitive regex pattern matching, use the 要执行不区分大小写的正则表达式模式匹配,请改用i Option instead. i选项。See 示例见i Option for an example.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模式包含捕获组,并且模式在输入中找到匹配项,则结果中的捕获数组对应于匹配字符串捕获的组。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: {
$regexFind: { 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" : null }
{ "_id" : 3, "returnObject" : null }
{ "_id" : 4, "returnObject" : { "match" : "Col", "idx" : 0, "captures" : [ "C", null ] } }
{ "_id" : 5, "returnObject" : null }
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. If a matching document is not captured by a group (e.g. Colleen and the group (ar)), $regexFind replaces the group with a null placeholder.captures数组中的元素对应于两个捕获组。如果一个组(例如Colleen和组(ar))没有捕获到匹配的文档,$regexFind会用空占位符替换该组。
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. Each group represents a New York City area code:or捕获组应用于phone字段来搜索具有纽约市区号的电话号码。每个组代表一个纽约市区号:
db.contacts.aggregate([
{
$project: {
nycContacts: {
$regexFind: { 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" : null }
{ "_id" : 4, "nycContacts" : null }
{ "_id" : 5, "nycContacts" : { "match" : "917-555-4414", "idx" : 0, "captures" : [ null, null, "917" ] } }Examples示例
$regexFind and Its Options及其选项
To illustrate the behavior of the 为了说明本例中讨论的$regexFind operator as discussed in this example, create a sample collection products with the following documents:$regexFind运算符的行为,请使用以下文档创建一个示例集合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, 默认情况下,$regexFind performs a case-sensitive match. $regexFind执行区分大小写的匹配。For example, the following aggregation performs a case-sensitive 例如,以下聚合对$regexFind on the description field. The regex pattern /line/ does not specify any grouping:description字段执行区分大小写的$regexFind。正则表达式模式/line/没有指定任何分组:
db.products.aggregate([
{ $addFields: { returnObject: { $regexFind: { input: "$description", regex: /line/ } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "returnObject" : null }
{ "_id" : 2, "description" : "First lines\nsecond line", "returnObject" : { "match" : "line", "idx" : 6, "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" : null }
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : null }
The following regex pattern 以下正则表达式模式/lin(e|k)/ specifies a grouping (e|k) in the pattern:/lin(e|k)/指定了模式中的分组(e|k):
db.products.aggregate([
{ $addFields: { returnObject: { $regexFind: { input: "$description", regex: /lin(e|k)/ } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "returnObject" : null }
{ "_id" : 2, "description" : "First lines\nsecond line", "returnObject" : { "match" : "line", "idx" : 6, "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" ] } }
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : null }
In the return option, the 在返回选项中,idx field is the code point index and not the byte index. To illustrate, consider the following example that uses the regex pattern /tier/:idx字段是代码点索引,而不是字节索引。为了说明这一点,请考虑以下使用正则表达式模式/tier/的示例:
db.products.aggregate([
{ $addFields: { returnObject: { $regexFind: { 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" : null }
{ "_id" : 2, "description" : "First lines\nsecond line", "returnObject" : null }
{ "_id" : 3, "description" : "Many spaces before line", "returnObject" : null }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returnObject" : null }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returnObject" : null }
{ "_id" : 6, "description" : "métier work vocation",
"returnObject" : { "match" : "tier", "idx" : 2, "captures" : [ ] } }
i Option选项
Note
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字段的一部分或包含在选项字段中:
// Specify i as part of the regex field将i指定为正则表达式字段的一部分
{ $regexFind: { input: "$description", regex: /line/i } }
// Specify i in the options field
{ $regexFind: { input: "$description", regex: /line/, options: "i" } }
{ $regexFind: { input: "$description", regex: "line", options: "i" } }
For example, the following aggregation performs a case-insensitive 例如,以下聚合对$regexFind on the description field. The regex pattern /line/ does not specify any grouping:description字段执行不区分大小写的$regexFind。正则表达式模式/line/没有指定任何分组:
db.products.aggregate([
{ $addFields: { returnObject: { $regexFind: { 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" : [ ] } }
{ "_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" : null }
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : null }m Option选项
Note
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
{ $regexFind: { input: "$description", regex: /line/m } }
// Specify m in the options field
{ $regexFind: { input: "$description", regex: /line/, options: "m" } }
{ $regexFind: { 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: { $regexFind: { 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" : null }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returnObject" : null }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returnObject" : null }
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : null }x Option选项
Note
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
{ $regexFind: { input: "$description", regex: /line/, options: "x" } }
{ $regexFind: { 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: { $regexFind: { input: "$description", regex: /lin(e|k) # matches line or link/, options:"x" } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "returnObject" : null }
{ "_id" : 2, "description" : "First lines\nsecond line", "returnObject" : { "match" : "line", "idx" : 6, "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" ] } }
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : null }s Option选项
Note
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
{ $regexFind: { input: "$description", regex: /m.*line/, options: "s" } }
{ $regexFind: { 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: { $regexFind: { input: "$description", regex:/m.*line/, options: "si" } } } }
])
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "description" : "Single LINE description.", "returnObject" : null }
{ "_id" : 2, "description" : "First lines\nsecond line", "returnObject" : null }
{ "_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" : null }
{ "_id" : 6, "description" : "métier work vocation", "returnObject" : null }Use $regexFind to Parse Email from String使用$regexFind从字符串解析电子邮件
$regexFind 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? cam@mongodb.com" },
{ "_id" : 4, comment: "It's just me. I'm testing. fred@MongoDB.com" }
])
The following aggregation uses the 以下聚合使用$regexFind to extract the email from the comment field (case insensitive).$regexFind从comment字段中提取电子邮件(不区分大小写)。
db.feedback.aggregate( [
{ $addFields: {
"email": { $regexFind: { input: "$comment", regex: /[a-z0-9_.+-]+@[a-z0-9_.+-]+\.[a-z0-9_.+-]+/i } }
} },
{ $set: { email: "$email.match"} }
] )
First Stage第一阶段The stage uses the该阶段使用$addFieldsstage to add a new fieldemailto the document.$addFields阶段将新的字段email添加到文档中。The new field contains the result of performing the新字段包含对$regexFindon thecommentfield:comment字段执行$regexFind的结果:{ "_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" : null }
{ "_id" : 3, "comment" : "I can't find how to convert a date to string. cam@mongodb.com", "email" : { "match" : "cam@mongodb.com", "idx" : 46, "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该阶段使用$setstage to reset theemailto the current"$email.match"value.$set阶段将email重置为当前的"$email.match"值。If the current value of如果emailis null, the new value ofemailis 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" }
{ "_id" : 3, "comment" : "I can't find how to convert a date to string. cam@mongodb.com", "email" : "cam@mongodb.com" }
{ "_id" : 4, "comment" : "It's just me. I'm testing. fred@MongoDB.com", "email" : "fred@MongoDB.com" }
Apply $regexFind to String Elements of an Array将$regexFind应用于数组的字符串元素
$regexFind to String Elements of an ArrayCreate a sample collection 使用以下文档创建样本集合contacts with the following documents:contacts:
db.contacts.insertMany([
{ "_id" : 1, name: "Aunt Arc Tikka", details: [ "+672-19-9999", "aunt.arc.tica@example.com" ] },
{ "_id" : 2, name: "Belle Gium", details: [ "+32-2-111-11-11", "belle.gium@example.com" ] },
{ "_id" : 3, name: "Cam Bo Dia", details: [ "+855-012-000-0000", "cam.bo.dia@example.com" ] },
{ "_id" : 4, name: "Fred", details: [ "+1-111-222-3333" ] }
])
The following aggregation uses the 以下聚合使用$regexFind to convert the details array into an embedded document with an email and phone fields:$regexFind将details数组转换为包含email和phone字段的嵌入式文档:
db.contacts.aggregate( [
{ $unwind: "$details" },
{ $addFields: {
"regexemail": { $regexFind: { input: "$details", regex: /^[a-z0-9_.+-]+@[a-z0-9_.+-]+\.[a-z0-9_.+-]+$/, options: "i" } },
"regexphone": { $regexFind: { input: "$details", regex: /^[+]{0,1}[0-9]*\-?[0-9_\-]+$/ } }
} },
{ $project: { _id: 1, name: 1, details: { email: "$regexemail.match", phone: "$regexphone.match" } } },
{ $group: { _id: "$_id", name: { $first: "$name" }, details: { $mergeObjects: "$details"} } },
{ $sort: { _id: 1 } }
])
First Stage第一阶段The stage阶段$unwindsthe array into separate documents:$unwind将数组展开为单独的文档:{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : "+672-19-9999" }
{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : "aunt.arc.tica@example.com" }
{ "_id" : 2, "name" : "Belle Gium", "details" : "+32-2-111-11-11" }
{ "_id" : 2, "name" : "Belle Gium", "details" : "belle.gium@example.com" }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : "+855-012-000-0000" }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : "cam.bo.dia@example.com" }
{ "_id" : 4, "name" : "Fred", "details" : "+1-111-222-3333" }Second Stage第二阶段The stage uses the该阶段使用$addFieldsstage to add new fields to the document that contains the result of the$regexFindfor phone number and email:$addFields阶段将新字段添加到包含电话号码和电子邮件的$regexFind结果的文档中:{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : "+672-19-9999", "regexemail" : null, "regexphone" : { "match" : "+672-19-9999", "idx" : 0, "captures" : [ ] } }
{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : "aunt.arc.tica@example.com", "regexemail" : { "match" : "aunt.arc.tica@example.com", "idx" : 0, "captures" : [ ] }, "regexphone" : null }
{ "_id" : 2, "name" : "Belle Gium", "details" : "+32-2-111-11-11", "regexemail" : null, "regexphone" : { "match" : "+32-2-111-11-11", "idx" : 0, "captures" : [ ] } }
{ "_id" : 2, "name" : "Belle Gium", "details" : "belle.gium@example.com", "regexemail" : { "match" : "belle.gium@example.com", "idx" : 0, "captures" : [ ] }, "regexphone" : null }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : "+855-012-000-0000", "regexemail" : null, "regexphone" : { "match" : "+855-012-000-0000", "idx" : 0, "captures" : [ ] } }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : "cam.bo.dia@example.com", "regexemail" : { "match" : "cam.bo.dia@example.com", "idx" : 0, "captures" : [ ] }, "regexphone" : null }
{ "_id" : 4, "name" : "Fred", "details" : "+1-111-222-3333", "regexemail" : null, "regexphone" : { "match" : "+1-111-222-3333", "idx" : 0, "captures" : [ ] } }Third Stage第三阶段The stage use the该阶段使用$projectstage to output documents with the_idfield, thenamefield and thedetailsfield. Thedetailsfield is set to a document withemailandphonefields, whose values are determined from theregexemailandregexphonefields, respectively.$project阶段输出具有_id字段、name字段和details字段的文档。details字段设置为具有email和phone字段的文档,其值分别由regexemail和regexphone字段确定。{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : { "phone" : "+672-19-9999" } }
{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : { "email" : "aunt.arc.tica@example.com" } }
{ "_id" : 2, "name" : "Belle Gium", "details" : { "phone" : "+32-2-111-11-11" } }
{ "_id" : 2, "name" : "Belle Gium", "details" : { "email" : "belle.gium@example.com" } }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : { "phone" : "+855-012-000-0000" } }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : { "email" : "cam.bo.dia@example.com" } }
{ "_id" : 4, "name" : "Fred", "details" : { "phone" : "+1-111-222-3333" } }Fourth Stage第四阶段The stage uses the该阶段使用$groupstage to groups the input documents by their_idvalue. The stage uses the$mergeObjectsexpression to merge thedetailsdocuments.$group阶段按_id值对输入文档进行分组。该阶段使用$mergeObjects表达式合并详细信息文档。{ "_id" : 3, "name" : "Cam Bo Dia", "details" : { "phone" : "+855-012-000-0000", "email" : "cam.bo.dia@example.com" } }
{ "_id" : 4, "name" : "Fred", "details" : { "phone" : "+1-111-222-3333" } }
{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : { "phone" : "+672-19-9999", "email" : "aunt.arc.tica@example.com" } }
{ "_id" : 2, "name" : "Belle Gium", "details" : { "phone" : "+32-2-111-11-11", "email" : "belle.gium@example.com" } }Fifth Stage第五阶段The stage uses the该阶段使用$sortstage to sort the documents by the_idfield.$sort阶段按_id字段对文档进行排序。{ "_id" : 1, "name" : "Aunt Arc Tikka", "details" : { "phone" : "+672-19-9999", "email" : "aunt.arc.tica@example.com" } }
{ "_id" : 2, "name" : "Belle Gium", "details" : { "phone" : "+32-2-111-11-11", "email" : "belle.gium@example.com" } }
{ "_id" : 3, "name" : "Cam Bo Dia", "details" : { "phone" : "+855-012-000-0000", "email" : "cam.bo.dia@example.com" } }
{ "_id" : 4, "name" : "Fred", "details" : { "phone" : "+1-111-222-3333" } }
Use Captured Groupings to Parse User Name使用捕获的分组来解析用户名
Create a sample collection 使用以下文档创建样本集合employees with the following documents:employees:
db.employees.insertMany([
{ "_id" : 1, name: "Aunt Arc Tikka", "email" : "aunt.tica@example.com" },
{ "_id" : 2, name: "Belle Gium", "email" : "belle.gium@example.com" },
{ "_id" : 3, name: "Cam Bo Dia", "email" : "cam.dia@example.com" },
{ "_id" : 4, name: "Fred" }
])
The employee email has the format 员工电子邮件的格式为<firstname>.<lastname>@example.com. Using the captured field returned in the $regexFind results, you can parse out user names for employees.<firstname>.<lastname>@example.com。使用$regexFind结果中返回的captured字段,您可以解析出员工的用户名。
db.employees.aggregate( [
{ $addFields: {
"username": { $regexFind: { input: "$email", regex: /^([a-z0-9_.+-]+)@[a-z0-9_.+-]+\.[a-z0-9_.+-]+$/, options: "i" } },
} },
{ $set: { username: { $arrayElemAt: [ "$username.captures", 0 ] } } }
] )
First Stage第一阶段The stage uses the该阶段使用$addFieldsstage to add a new fieldusernameto the document. The new field contains the result of performing the$regexFindon theemailfield:$addFields阶段将新的字段username添加到文档中。新字段包含对email字段执行$regexFind的结果:{ "_id" : 1, "name" : "Aunt Arc Tikka", "email" : "aunt.tica@example.com", "username" : { "match" : "aunt.tica@example.com", "idx" : 0, "captures" : [ "aunt.tica" ] } }
{ "_id" : 2, "name" : "Belle Gium", "email" : "belle.gium@example.com", "username" : { "match" : "belle.gium@example.com", "idx" : 0, "captures" : [ "belle.gium" ] } }
{ "_id" : 3, "name" : "Cam Bo Dia", "email" : "cam.dia@example.com", "username" : { "match" : "cam.dia@example.com", "idx" : 0, "captures" : [ "cam.dia" ] } }
{ "_id" : 4, "name" : "Fred", "username" : null }Second Stage第二阶段The stage use the该阶段使用$setstage to reset theusernameto the zero-th element of the"$username.captures"array. If the current value ofusernameis null, the new value ofusernameis set to null.$set阶段将username重置为"$username.captures"数组的第零个元素。如果username的当前值为空,则username的新值设置为空。{ "_id" : 1, "name" : "Aunt Arc Tikka", "email" : "aunt.tica@example.com", "username" : "aunt.tica" }
{ "_id" : 2, "name" : "Belle Gium", "email" : "belle.gium@example.com", "username" : "belle.gium" }
{ "_id" : 3, "name" : "Cam Bo Dia", "email" : "cam.dia@example.com", "username" : "cam.dia" }
{ "_id" : 4, "name" : "Fred", "username" : null }
Tip
For more information on the behavior of the 有关captures array and additional examples, see captures Output Behavior.captures数组行为和其他示例的更多信息,请参阅captures输出行为。