定義
$replaceAllinput string内の検索stringまたは正規表現パターンのすべてのインスタンスを置換stringで置き換えます。
$replaceAllは大文字と小文字、および発音区別符号を区別し、コレクションに存在する照合を無視します。
構文
The $replaceAll operator has the following operator expression syntax:
{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }
演算子フィールド
フィールド | 説明 |
|---|---|
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, a regex, 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 regex for find), or $replaceAll fails with an error.
$replaceAll および NULL 値
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 式の string マッチングは、常に大文字と小文字を区別し、発音区別符号も区別します。で string 比較を実行する場合、構成された照合は無視されます。$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é" }
$match のように照合を尊重する演算子は、"Cafe" で文字列比較を実行すると、3 つのドキュメントすべてに一致することになります。これは、このコレクションの照合強度が 1 であるためです。
$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.
例、文字 é は、1 つのコード ポイントまたは 2 つのコード ポイントを使用して 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" }, ] )
string を使用した置換
次の例では、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\\b を使用して、itemフィールド内のインスタンス全体を「赤色の塗料」に置き換えます。
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)が使用されているため、フレーズ全体として表示された場合にのみ置き換えられ、より大きな単語内の部分文字列である場合は置き換えられません。