$replaceOne (aggregation)
定义
$replaceOne
Replaces the first instance of a search string in an input string with a replacement string.
If no occurrences are found,
$replaceOne
evaluates to the input string.$replaceOne
区分大小写和变音符号,并忽略集合上存在的任何排序规则。
语法
$replaceOne
操作符采用以下操作符表达式语法:
{ $replaceOne: { input: <expression>, find: <expression>, replacement: <expression> } }
Operator 徽标
字段 | 说明 |
---|---|
要应用find 的字符串。可以是解析为字符串或 | |
The string to use to replace the first matched instance of
find in 输入.
Can be any valid 表达式(expression) that
resolves to a string or a |
行为
If no occurrences of find are found in
输入, $replaceOne
evaluates to
the input string.
input、find 和 replacement 表达式的计算结果必须为字符串或 null
,否则 $replaceOne
会失败并出现错误。
$replaceOne
和空值
如果 input 或 find 引用的字段缺失,则返回 null
。
如果 input、find 或 replacement 中的任何一个 计算结果为null
,则整个 $replaceOne
表达式的计算结果为
null
:
例子 | 结果 |
---|---|
|
|
|
|
|
|
$replaceOne
和排序规则
String matching for all $replaceOne
expressions is always
case-sensitive and diacritic-sensitive. Any 排序规则
configured is ignored when performing string comparisons with
$replaceOne
.
例如,创建一个排序规则强度为 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é" } ])
The following $replaceOne
operation tries to find and
replace the first instance of "Cafe" in the name
field:
db.myColl.aggregate([ { $addFields: { resultObject: { $replaceOne: { input: "$name", find: "Cafe", replacement: "CAFE" } } } } ])
由于 $replaceOne
忽略为此集合配置的排序规则,因此该操作仅与文档 2
中的“Cafe”实例匹配:
{ "_id" : 1, "name" : "cafe", "resultObject" : "cafe" } { "_id" : 2, "name" : "Cafe", "resultObject" : "CAFE" } { "_id" : 3, "name" : "café", "resultObject" : "café" }
由于该集合的排序规则强度为1
,因此遵循排序规则的操作符(例如
$match
)在与“Cafe”进行字符串比较时将匹配所有三个文档。
$replaceOne
与 Unicode 规范化
$replaceOne
聚合表达式不执行任何 unicode 规范化。这意味着在尝试匹配时,所有 $replaceOne
表达式的字符串匹配都将考虑 unicode 中用于表示字符的码点数量。
例如,字符é
可以使用一个或两个代码点以 unicode 表示:
Unicode | 显示为 | 代码点 |
---|---|---|
|
| 1 ( |
|
| 2 ( |
将 $replaceOne
与 find 字符串一起使用,其中字符 é
用 unicode 中的一个代码点表示,将不匹配在 input 字符串中使用两个代码点的任何
é
实例。
下表显示了 find 字符串 "café" 与 input 字符串进行比较时是否会出现匹配情况,其中 é
由一个或两个代码点表示。本例中的 find 字符串使用一个代码点来表示 é
字符:
例子 | 匹配 |
---|---|
| 是 |
| no |
由于 $replaceOne
不执行任何 Unicode 规范化,因此只有第一个字符串比较是匹配项,其中 find 和 input 字符串都使用一个码位来表示 é
。
例子
使用以下文档创建 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" }, ])
The following example replaces the first instance of "blue paint" in the
item
field with "red paint":
db.inventory.aggregate([ { $project: { item: { $replaceOne: { 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 blue paintbrush" } { "_id" : 4, "item" : "red paint with green paintbrush" }
Note that with document 3
, only the first matched instance of
"blue paint" is replaced.