On this page本页内容
$replaceAll
¶New in version 4.4.版本4.4中的新功能。
Replaces all instances of a search string in an input string with a replacement string.用替换字符串替换输入字符串中搜索字符串的所有实例。
$replaceAll
is both case-sensitive and diacritic-sensitive, and ignores any collation present on a collection.区分大小写,区分重音,并忽略集合中存在的任何排序规则。
The $replaceAll
operator has the following operator expression syntax:$replaceAll
运算符具有以下运算符表达式语法:
input |
|
find |
|
replacement |
|
The input, find, and replacement expressions must evaluate to a string or a null
, or $replaceAll
fails with an error.input
、find
和replacement
表达式的计算结果必须为字符串或null
,否则$replaceAll
失败并出现错误。
$replaceAll
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 $replaceAll
expression evaluates to null
:input
、find
或replacement
中的任何一个计算结果为null
,则整个$replaceAll
表达式的计算结果为null
:
Result | |
---|---|
{ $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
and Collation¶String matching for all $replaceAll
expressions is always case-sensitive and diacritic-sensitive. Any collation configured on a collection, db.collection.aggregate()
, or index is ignored when performing string comparisons with $replaceAll
.
For example, create a sample collection with collation strength 例如,创建排序规则强度为1
: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:接下来,插入三个示例文档:
The following $replaceAll
operation tries to find and replace all instances of “Cafe” in the name
field:
Because 由于$replaceAll
ignores the collation configured for this collection, the operation only matches the instance of “Cafe” in document 2
:$replaceAll
忽略为此集合配置的排序规则,因此该操作只匹配文档2
中“Cafe”的实例:
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
.
$replaceAll
and Unicode Normalization¶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.
For example, the character é
can be represented in unicode using either one code point or two:
Unicode | ||
---|---|---|
\xe9 |
é |
1 ( \xe9 ) |
e\u0301 |
é |
2 ( e + \u0301 ) |
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:
Match | |
---|---|
{ $replaceAll: { input: "caf\xe9", find: "café", replacement: "CAFE" } } |
yes |
{ $replaceAll: { input: "cafe\u0301", find: "café", replacement: "CAFE" } } |
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 é
.
Create an inventory
collection with the following documents:
The following example replaces each instance of “blue paint” in the item
field with “red paint”:
The operation returns the following results:操作返回以下结果: