定义
$replaceAll将输入字符串中搜索字符串的所有实例替换为替换字符串。
$replaceAll区分大小写,区分变音符号,忽略集合上存在的任何排序规则。
语法
The $replaceAll operator has the following operator expression syntax:
{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }
Operator 徽标
字段 | 说明 |
|---|---|
The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a | |
The string to search for within the given input. Can be any valid expression that resolves to a string or a | |
The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a |
行为
The input, find, and replacement expressions must evaluate to a string or a null, or $replaceAll fails with an error.
$replaceAll 和空值
If input or find refer to a field that is missing, they return null.
If any one of input, find, or replacement evaluates to a null, the entire $replaceAll expression evaluates to null:
例子 | 结果 |
|---|---|
|
|
|
|
|
|
$replaceAll 和排序规则
$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é" }
Because $replaceAll ignores the collation configured for this collection, the operation only matches the instance of "Cafe" in document 2:
{ _id: 1, name: "cafe", resultObject: "cafe" } { _id: 2, name: "Cafe", resultObject: "CAFE" } { _id: 3, name: "café", resultObject: "café" }
由于该集合的排序规则强度为 1,因此遵循排序规则的操作符(例如 $match)在与“Cafe”执行字符串比较时会匹配所有三个文档。
$replaceAll 与 Unicode 规范化
The $replaceAll aggregation expression does not perform any unicode normalization. This means that string matching for all $replaceAll expressions will consider the number of code points used to represent a character in unicode when attempting a match.
例如,字符é可以使用一个或两个代码点以 unicode 表示:
Unicode | 显示为 | 代码点 |
|---|---|---|
|
| 1 ( |
|
| 2 ( |
Using $replaceAll with a find string where the character é is represented in unicode with one code point will not match any instance of é that uses two code points in the input string.
The following table shows whether a match occurs for a find string of "café" when compared to input strings where é is represented by either one code point or two. The find string in this example uses one code point to represent the é character:
例子 | 匹配 |
|---|---|
| 是 |
| no |
Because $replaceAll does not perform any unicode normalization, only the first string comparison matches, where both the find and input strings use one code point to represent é.
例子
使用以下文档创建 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" }