Docs Menu

$replaceAll (aggregation)

$replaceAll

Replaces all instances of a search string in an input string with a replacement string.

$replaceAllは大文字と小文字を区別し、発音区別符号も区別し、コレクションに存在する照合を無視します。

$replaceAll演算子には次の演算子式の構文があります。

{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }
フィールド
説明

find を適用する string 。string または に解決される任意の有効な式を指定できます。nullinput が欠落しているフィールドを参照する場合、$replaceAllnull を返します。

指定された入力 内で検索する string です。string または に解決される任意の有効な式を指定できます。nullfind が欠落しているフィールドを参照する場合、$replaceAllnull を返します。

The string to use to replace all matched instances of find in 入力. Can be any valid that resolves to a string or a null.

inputfind 、および置換式は、string またはnullに評価される必要があります。または$replaceAllはエラーで失敗します。

inputまたはfindが欠落しているフィールドを参照する場合、 nullが返されます。

input find 、または 置換 いずれかnull と評価されると、 式全体が$replaceAll と評価されます。null

結果

{ $replaceAll: { input: null, find: "abc", replacement: "ABC" } }

null

{ $replaceAll: { input: "abc", find: null, replacement: "ABC" } }

null

{ $replaceAll: { input: "abc", find: "abc", replacement: null } }

null

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.

たとえば、以下は照合強度 1 のサンプル コレクションを作成します。

db.createCollection( "myColl", { collation: { locale: "fr", strength: 1 } } )

照合強度が 1 の場合、基本文字のみが比較され、大文字と小文字や発音区別符号などの他の違いは無視されます。

次に、次の 3 つのサンプル ドキュメントを挿入します。

db.myColl.insertMany([
{ _id: 1, name: "cafe" },
{ _id: 2, name: "Cafe" },
{ _id: 3, name: "café" }
])

The following $replaceAll operation tries to find and replace all instances of "Cafe" in the name field:

db.myColl.aggregate([
{
$addFields:
{
resultObject: { $replaceAll: { input: "$name", find: "Cafe", replacement: "CAFE" } }
}
}
])

$replaceAllはこのコレクションに構成された照合を無視するため、操作はドキュメント2の "Cafe" のインスタンスのみと一致します。

{ "_id" : 1, "name" : "cafe", "resultObject" : "cafe" }
{ "_id" : 2, "name" : "Cafe", "resultObject" : "CAFE" }
{ "_id" : 3, "name" : "café", "resultObject" : "café" }

$match のように照合を尊重する演算子は、"Cafe" で文字列比較を実行すると、3 つのドキュメントすべてに一致することになります。これは、このコレクションの照合強度が 1 であるためです。

$replaceAll集計式は Unicode 正規化を実行しません。 つまり、すべての$replaceAll式の string マッチングでは、一致を試みるときに Unicode で文字を表すのに使用されるコード ポイントの数が考慮されます。

例、文字 é は、1 つのコード ポイントまたは 2 つのコード ポイントを使用して Unicode で表すことができます。

Unicode
表示方法
コード ポイント

\xe9

é

1 ( \xe9 )

e\u0301

é

2 ( e + \u0301 )

文字 が 1 つのコード ポイントを持つ Unicode で表される 検索$replaceAll で を使用しても、stringéé 入力 に 2 つのコード ポイントを使用するstring のインスタンスと一致しません。

次の表は、 が 1 つのコード ポイントまたは 2 つのコード ポイントで表される 入力 文字列と比較して、"Cafe" の 検索 に一致が発生するかどうかを示しています。stringéこの例の検索stringは、1 つのコード ポイントを使用して é 文字を表します。

一致

{ $replaceAll: { input: "caf\xe9", find: "café", replacement: "CAFE" } }

はい

{ $replaceAll: { input: "cafe\u0301", find: "café", replacement: "CAFE" } }

no

$replaceAllは Unicode 正規化を実行しないため、最初の string 比較のみが一致します。ここでは、検索文字列と入力文字列の両方がéを表すために 1 つのコード ポイントを使用します。

次のドキュメントを使用して 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 each instance of "blue paint" in the item field with "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" }