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

$replaceAll(聚合)

在此页面上

  • 定义
  • 语法
  • 行为
  • 例子
$replaceAll

将输入字符串中搜索字符串的所有实例替换为替换字符串。

$replaceAll 区分大小写,区分变音符号,忽略集合上存在的任何排序规则。

$replaceAll操作符具有以下操作符表达式语法:

{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }
字段
说明
输入

您要对其应用 find 的字符串。可以是解析为字符串或 null 的任何有效表达式。如果 input 引用一个缺失的字段,则 $replaceAll 返回 null

给定 input 中待搜索的字符串。可以是解析为字符串或 null 的任何有效表达式。如果 find 引用一个缺失的字段,则 $replaceAll 返回 null

用于替换输出中所有匹配的查找实例的字符串。可以是解析为字符串或 null 的任何有效表达式

inputfindreplacement 表达式的计算结果必须为字符串或 null,否则 $replaceAll 会失败并出现错误。

如果 inputfind 引用的字段缺失,则返回 null

如果 inputfindreplacement 中的任何一个 计算结果为null,则整个 $replaceAll 表达式的计算结果为 null

例子
结果
{ $replaceAll: { input: null, find: "abc", replacement: "ABC" } }
null
{ $replaceAll: { input: "abc", find: null, replacement: "ABC" } }
null
{ $replaceAll: { input: "abc", find: "abc", replacement: null } }
null

所有 $replaceAll 表达式的字符串匹配始终区分大小写且区分变音符号。$replaceAll 进行字符串比较时,会忽略对集合、db.collection.aggregate() 或索引配置的所有排序规则

例如,创建一个排序规则强度为 1 的样本集合:

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

排序规则强度 1 仅比较基本字符并忽略其他差异,例如大小写和变音符号。

接下来,插入三个示例文档:

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

以下 $replaceAll 操作尝试在 name 字段中查找并替换 "Cafe" 的所有实例:

db.myColl.aggregate([
{
$addFields:
{
resultObject: { $replaceAll: { input: "$name", find: "Cafe", replacement: "CAFE" } }
}
}
])

由于 $replaceAll 忽略为此集合配置的排序规则,因此该操作仅与文档 2 中的“Cafe”实例匹配:

{ "_id" : 1, "name" : "cafe", "resultObject" : "cafe" }
{ "_id" : 2, "name" : "Cafe", "resultObject" : "CAFE" }
{ "_id" : 3, "name" : "café", "resultObject" : "café" }

由于该集合的排序规则强度为1,因此遵循排序规则的操作符(例如 $match)在与“Cafe”进行字符串比较时将匹配所有三个文档。

$replaceAll 聚合表达式不执行任何 unicode 规范化。这意味着在尝试匹配时,所有 $replaceAll 表达式的字符串匹配都将考虑 unicode 中用于表示字符的码点数量。

例如,字符é可以使用一个或两个代码点以 unicode 表示:

Unicode
显示为
代码点
\xe9
é
1 ( \xe9 )
e\u0301
é
2 ( e + \u0301 )

$replaceAllfind 字符串一起使用,其中字符 é 用 unicode 中的一个代码点表示,将不匹配在 input 字符串中使用两个代码点的任何 é 实例。

下表显示了 find 字符串 "café" 与 input 字符串进行比较时是否会出现匹配情况,其中 é 由一个或两个代码点表示。本例中的 find 字符串使用一个代码点来表示 é 字符:

例子
匹配
{ $replaceAll: { input: "caf\xe9", find: "café", replacement: "CAFE" } }
{ $replaceAll: { input: "cafe\u0301", find: "café", replacement: "CAFE" } }
没有

由于 $replaceAll 不执行任何 Unicode 规范化,因此只有第一个字符串比较是匹配项,其中 findinput 字符串都使用一个码位来表示 é

使用以下文档创建 inventory 集合:

db.inventory.insertMany([
{ "_id" : 1, "item" : "blue paint" },
{ "_id" : 2, "item" : "blue and green paint" },
{ "_id" : 3, "item" : "blue paint with blue paintbrush" },
{ "_id" : 4, "item" : "blue paint with green paintbrush" },
])

以下示例将 item 字段中“blue paint”的每个实例替换为“red paint”:

db.inventory.aggregate([
{
$project:
{
item: { $replaceAll: { input: "$item", find: "blue paint", replacement: "red paint" } }
}
}
])

操作返回以下结果:

{ "_id" : 1, "item" : "red paint" }
{ "_id" : 2, "item" : "blue and green paint" }
{ "_id" : 3, "item" : "red paint with red paintbrush" }
{ "_id" : 4, "item" : "red paint with green paintbrush" }

后退

$replaceOne(聚合)

来年

$reverseArray(聚合)