$replaceOne (aggregation)
On this page本页内容
Definition定义
$replaceOne
New in version 4.4.4.4版新增。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
is both case-sensitive and diacritic-sensitive, and ignores any collation present on a collection.区分大小写和变音符号,并忽略集合中存在的任何排序规则。
Syntax语法
The $replaceOne
operator has the following operator expression syntax:$replaceOne
运算符具有以下运算符表达式语法:
{ $replaceOne: { input: <expression>, find: <expression>, replacement: <expression> } }
Operator Fields运算符字段
input | null . null 的任何有效表达式。input refers to a field that is missing, $replaceOne returns null . input 引用的字段丢失,$replaceOne 将返回null 。 |
find | input 中搜索的字符串。null . null 的任何有效表达式。find refers to a field that is missing, $replaceOne returns null . find 引用的字段丢失,$replaceOne 将返回null 。 |
replacement | input 中查找的第一个匹配实例的字符串。null . null 的任何有效表达式。 |
Behavior行为
If no occurrences of find are found in input, 如果在$replaceOne
evaluates to the input string.input
中找不到find
,$replaceOne
将计算为输入字符串。
The input, find, and replacement expressions must evaluate to a string or a null
, or $replaceOne
fails with an error.input
表达式、find
表达式和replacement
表达式的计算结果必须为字符串或null
,或者$replaceOne
失败并返回错误。
$replaceOne
and Null Values和Null
值
If input or find refer to a field that is missing, they return 如果null
.input
或find
引用了缺失的字段,则返回null
。
If any one of input, find, or replacement evaluates to a 如果null
, the entire $replaceOne
expression evaluates to null
:input
、find
或replacement
中的任何一个的计算结果为null
,则整个$replaceOne
表达式的计算结果将为null
:
{ $replaceOne: { input: null, find: "abc", replacement: "ABC" } } | null |
{ $replaceOne: { input: "abc", find: null, replacement: "ABC" } } | null |
{ $replaceOne: { input: "abc", find: "abc", replacement: null } } | null |
$replaceOne
and Collation和排序规则
String matching for all 所有$replaceOne
expressions is always case-sensitive and diacritic-sensitive. $replaceOne
表达式的字符串匹配始终区分大小写和变音符号。Any collation configured is ignored when performing string comparisons with 与$replaceOne
.$replaceOne
执行字符串比较时,将忽略任何配置的collation
。
For example, create a sample collection with collation strength 例如,创建排序规则强度为1
:1
:
db.createCollection( "myColl", { collation: { locale: "fr", strength: 1 } } )
A collation strength of 排序规则强度为1
compares base character only and ignores other differences such as case and diacritics.1
时,只比较基本字符,而忽略其他差异,如大小写和变音符号。
Next, insert three example documents:接下来,插入三个示例文档:
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:$replaceOne
操作尝试在name
字段中查找并替换“Cafe”的第一个实例:
db.myColl.aggregate([
{
$addFields:
{
resultObject: { $replaceOne: { input: "$name", find: "Cafe", replacement: "CAFE" } }
}
}
])
Because 由于$replaceOne
ignores the collation configured for this collection, the operation only matches the instance of "Cafe" in document 2
:$replaceOne
忽略为此集合配置的排序规则,因此该操作仅与文档2
中的“Cafe”实例匹配:
{ "_id" : 1, "name" : "cafe", "resultObject" : "cafe" }
{ "_id" : 2, "name" : "Cafe", "resultObject" : "CAFE" }
{ "_id" : 3, "name" : "café", "resultObject" : "café" }
Operators which respect collation, such as 尊重排序规则的运算符(如$match
, would match all three documents when performing a string comparison against "Cafe" due to this collection's collation strength of 1
.$match
)在与“Cafe”进行字符串比较时会匹配所有三个文档,因为该集合的排序规则强度为1。
$replaceOne
and Unicode Normalization和Unicode规范化
The $replaceOne
aggregation expression does not perform any unicode normalization. $replaceOne
聚合表达式不执行任何unicode规范化。This means that string matching for all 这意味着,在尝试匹配时,所有$replaceOne
expressions will consider the number of code points used to represent a character in unicode when attempting a match.$replaceOne
表达式的字符串匹配都将考虑用于表示unicode中字符的代码点数。
For example, the character 例如,字符é
can be represented in unicode using either one code point or two:é
可以使用一个或两个代码点在unicode中表示:
Unicode | ||
---|---|---|
\xe9 | é | 1 ( \xe9 ) |
e\u0301 | é | 2 ( e + \u0301 ) |
Using 将$replaceOne
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.$replaceOne
与find
字符串(其中字符é
在unicode中用一个代码点表示)一起使用,将不会与input
字符串中使用两个代码点的任何é
实例匹配。
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. find
字符串é
相比,find字符串“café”是否匹配。The find string in this example uses one code point to represent the 本例中的find字符串使用一个代码点来表示字符é
character:é
:
{ $replaceOne: { input: "caf\xe9", find: "café", replacement: "CAFE" } } | yes |
{ $replaceOne: { input: "cafe\u0301", find: "café", replacement: "CAFE" } } | no |
Because 由于$replaceOne
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 é
.$replaceOne
不执行任何unicode规范化,因此只有第一个字符串比较匹配,其中find
字符串和input
字符串都使用一个代码点来表示é
。
Example实例
Create an 使用以下文档创建inventory
collection with the following documents: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":item
字段中“蓝色油漆”的第一个实例替换为“红色油漆”:
db.inventory.aggregate([
{
$project:
{
item: { $replaceOne: { input: "$item", find: "blue paint", replacement: "red paint" } }
}
}
])
The operation returns the following results:该操作返回以下结果:
{ "_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.3
中,只有“bluepaint”的第一个匹配实例被替换。