“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$regexMatch(聚合)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例
$regexMatch

执行正则表达式 (regex) 模式匹配并返回:

  • true 如果存在匹配项。

  • false 如果不存在匹配项。

MongoDB 使用与 Perl 兼容的正则表达式(即 "PCRE" ) 版本 8.41,支持 UTF-8。

$regexMatch 操作符的语法如下:

{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }
字段
说明
输入

要应用正则表达式模式的字符串。可以是字符串或解析为字符串的任何有效表达式

要应用的正则表达式模式。 可以是解析为字符串或正则表达式模式/<pattern>/的任何有效表达式。使用正则表达式/<pattern>/时,您还可以指定正则表达式选项im (但不能指定sx选项):

  • "pattern"

  • /<pattern>/

  • /<pattern>/<options>

或者,您也可以使用选项字段指定正则表达式选项。要指定 sx 选项,必须使用选项字段。

不能同时在 regexoptions 字段中指定选项。

可选。以下 <options> 可用于正则表达式。

注意

不能同时在 regexoptions 字段中指定选项。

选项
说明
i
大小写不敏感,可同时匹配大写和小写。可以在 options 字段中或作为正则表达式字段的一部分指定该选项。
m

对于包含锚点的模式(即 ^ 表示开头,$ 表示结尾),在每行的开头或结尾匹配具有多行值的字符串。如果没有此选项,这些锚点将匹配字符串的开头或结尾。

如果模式不包含锚点,或者字符串值没有换行符(如 \n),则 m 选项没有任何作用。

x

“扩展”功能将忽略模式中的所有空白字符,除非转义或包含在字符类中。

此外,其还会忽略未转义的哈希/磅 (#) 字符和下一新行(含)之间的字符,因此您可以在复杂的模式中加入注释。这种情况只适用于数据字符;空白字符绝不能出现在模式中的特殊字符序列中。

x 选项不影响对 VT 字符的处理(如代码 11)。

您只能在 options 字段中指定该选项。

s

允许点字符(即 .)匹配所有字符,包括换行符。

您只能在 options 字段中指定该选项。

操作符返回一个布尔值:

  • true 如果存在匹配项。

  • false 如果不存在匹配项。

提示

另请参阅:

$regexMatch 忽略为集合 db.collection.aggregate() 和索引(如使用)指定的排序规则。

例如,创建一个排序规则强度为 1 的样本集合(即仅比较基本字符,忽略其他差异,例如大小写和变音符号):

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

插入以下文档:

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

使用集合的排序规则,以下操作执行不区分大小写和不区分变音符号的匹配:

db.myColl.aggregate( [ { $match: { category: "cafe" } } ] )

该操作将返回以下 3 个文档:

{ "_id" : 1, "category" : "café" }
{ "_id" : 2, "category" : "cafe" }
{ "_id" : 3, "category" : "cafE" }

但是,聚合表达式 $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
)

这两个操作都返回以下内容:

{ "_id" : 1, "category" : "café", "results" : false }
{ "_id" : 2, "category" : "cafe", "results" : true }
{ "_id" : 3, "category" : "cafE", "results" : false }

要执行不区分大小写的 regex 模式匹配,请改用 i 选项。有关示例,请参阅 i 选项

为了说明该示例中讨论的 $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" }
])

默认情况下,$regexMatch 执行区分大小写的匹配。例如,以下聚合在 description 字段上执行区分大小写$regexMatch。正则表达式模式 /line/ 不指定任何分组:

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /line/ } } } }
])

该操作返回以下内容:

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

以下正则表达式模式 /lin(e|k)/ 在模式中指定分组 (e|k)

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /lin(e|k)/ } } } }
])

该操作返回以下内容:

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

注意

不能同时在 regexoptions 字段中指定选项。

要执行不区分大小写的模式匹配,将 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" } }

例如,以下聚合在 description 字段上执行不区分大小写$regexMatch。正则表达式模式 /line/ 不指定任何分组:

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /line/i } } } }
])

该操作将返回以下文档:

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

注意

不能同时在 regexoptions 字段中指定选项。

要匹配多行字符串中每一行的指定锚点(如 ^$ ),请在 regex 字段或选项字段中包含 m 选项:

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

以下示例同时包含 im 选项,用于为多行字符串匹配以字母 sS 开头的行:

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /^s/im } } } }
])

该操作返回以下内容:

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

注意

不能同时在 regexoptions 字段中指定选项。

要忽略模式中所有未转义的空格字符和注释(由未转义的哈希 # 字符和下一个换行符表示),请在选项字段中包含 s 选项:

// Specify x in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "x" } }
{ $regexMatch: { input: "$description", regex: "line", options: "x" } }

以下示例纳入 x 选项来跳过非转义空格和注释:

db.products.aggregate([
{ $addFields: { returns: { $regexMatch: { input: "$description", regex: /lin(e|k) # matches line or link/, options:"x" } } } }
])

该操作返回以下内容:

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

注意

不能同时在 regexoptions 字段中指定选项。

要支持模式中的点字符(即 .)匹配包括换行符在内的所有字符,请在选项字段中加入 s 选项:

// Specify s in the options field
{ $regexMatch: { input: "$description", regex: /m.*line/, options: "s" } }
{ $regexMatch: { input: "$description", regex: "m.*line", options: "s" } }

下面的示例包含 s 选项,允许使用点字符(即“.”)来匹配包括新行在内的所有字符,以及使用 i 选项来执行不区分大小写的匹配:

db.products.aggregate([
{ $addFields: { returns: { $regexMatch: { input: "$description", regex:/m.*line/, options: "si" } } } }
])

该操作返回以下内容:

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

使用以下文档创建样本collectionfeedback

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

以下聚合使用 $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" } }
} },

该操作将返回以下文档:

{ "_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" }
← $regexFindAll(聚合)