对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$replaceAll(表达式操作符)

$replaceAll

用替换string替换input string中搜索string或正则表达式模式的所有实例。

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

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

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

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

要在给定输入中搜索的字符串。可以是解析为字符串的任何有效表达式、正则表达式或 null。如果find 引用缺失的字段,则$replaceAll 返回null

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

输入、查找和替换表达式的计算结果必须为字符串或 null(或 find 的正则表达式),否则$replaceAll 会失败并显示错误。

如果 input find 引用了缺失的字段,则会返回null

如果输入、查找或替换中任一项的计算结果为 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 执行字符串比较时,配置的任何排序规则都将被忽略。

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

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

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

接下来,插入示例文档:

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

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

db.restaurants.aggregate( [
{
$addFields:
{
resultObject: {
$replaceOne: {
input: "$name",
find: "Cafe",
replacement: "CAFE"
}
}
}
}
] )
{ "_id" : 1, "name" : "cafe", "resultObject" : "cafe" }
{ "_id" : 2, "name" : "Cafe", "resultObject" : "CAFE" }
{ "_id" : 3, "name" : "café", "resultObject" : "café" }

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

{ _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 )

$replaceAllé é与查找字符串一起使用,其中字符 在具有一个代码点的 unicode 中表示,将与输入字符串中使用两个代码点的 的任何实例不匹配。

é下表显示了“café”的查找字符串是否与“a”匹配。与输入字符串相比,其中 由一个或两个代码点表示。此示例中的查找字符串使用一个代码点来表示 é字符:

例子
匹配

{ $replaceAll: { input: "caf\xe9", find: "café", replacement: "CAFE" } }

{ $replaceAll: { input: "cafe\u0301", find: "café", replacement: "CAFE" } }

no

由于$replaceAll 不执行任何 unicode 规范化,因此仅在第一个字符串比较时匹配,其中查找字符串和输入字符串都使用一个代码点来表示é

使用以下文档创建 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" }

以下示例使用正则表达式模式\\bblue paint\\bitem字段中与作为整个短语的“bluepaint”匹配的每个实例替换为“redpaint”:

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

操作返回以下结果:

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

在本例中,由于使用了正则表达式单词边界 (\b),“bluepaint”仅在作为整个短语出现时才会被替换,而当它是较大单词中的子字符串时则不会被替换。

给本页内容打分