文档菜单
文档首页
/
MongoDB 手册
/ / /

$regexMatch(聚合)

本页

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

执行正则表达式(regex)模式匹配,如果存在匹配则返回

  • true

  • 如果不存在匹配则返回 false

操作符$regexMatch的语法如下

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

要应用正则表达式的字符串。可以是字符串或任何有效的表达式,其结果为字符串。

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

  • "pattern"

  • /<pattern>/

  • /<pattern>/<options>

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

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

可选。以下可供使用的 <options> 可与正则表达式一起使用。

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

选项
描述
i
不区分大小写匹配大小写。您可以在 options 字段中指定此选项或将其作为正则表达式的一部分。
m

对于包含锚点的模式(例如,^ 表示开始,$ 表示结束),在具有多行值的字符串的开始或结束处进行匹配。没有此选项时,这些锚点将在字符串的开始或结束处匹配。

如果模式不包含锚点或字符串值不包含换行字符(例如,\n),则 m 选项无效。

x

“扩展”功能可以忽略模式中的所有空白字符,除非这些字符被转义或包含在字符集中。

此外,它还会忽略在未转义的反斜杠/井号(#)字符和下一个新行之间的字符,这样您可以在复杂的模式中包含注释。这仅适用于数据字符;空白字符不得出现在模式的特殊字符序列中。

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

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

s

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

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

操作符返回布尔值

  • true

  • 如果不存在匹配则返回 false

提示

另请参阅

从版本 6.1 开始,MongoDB 使用 PCRE2(Perl 兼容正则表达式)库来实现正则表达式模式匹配。要了解有关 PCRE2 的更多信息,请参阅 PCRE 文档。

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

要执行不区分大小写的正则表达式模式匹配,请使用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 选项作为 regex 字段或 options 字段的一部分包含在内

// 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字段中的选项。

要匹配多行字符串中每行的指定锚点(例如 ^$),请将 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" } }

以下示例同时包含 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选项包含在options字段中

// 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选项包含在options字段中

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

创建一个包含以下文档的示例集合 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" }
])

以下聚合操作使用$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

© . All rights reserved.