定義
$replaceOneinput string内の検索stringまたは正規表現パターンの最初のインスタンスを置換stringで置き換えます。
出現するものが見つからない場合、 は入力 string
$replaceOneとして評価されます。$replaceOneは大文字と小文字、および発音区別符号を区別し、コレクションに存在する照合を無視します。
構文
$replaceOne演算子には次の式子式の構文があります。
{ $replaceOne: { input: <expression>, find: <expression>, replacement: <expression> } }
演算子フィールド
フィールド | 説明 |
|---|---|
検索を適用する string。 string または に解決される任意の有効な式を指定できます。 | |
指定された入力内で検索する string。 string、regex、または に解決される任意の有効な式を指定できます。 | |
入力内で find に最初に一致したインスタンスを置き換えるために使用する string です。 string または |
動作
入力内に find の出現が見つからない場合、 $replaceOneは入力 string として評価されます。
入力式、検索式、および置換式は、次のいずれかの出力タイプに評価される必要があります。
A string
null
それ以外の場合、$replaceOne はエラーで失敗します。
$replaceOne および NULL 値
input または find が欠落しているフィールドを参照する場合、null が返されます。
入力、検索、または置換のいずれかが nullと評価されると、 式全体が$replaceOne と評価されます。null
例 | 結果 |
|---|---|
|
|
|
|
|
|
$replaceOne と照合
$replaceOne 式の string マッチングは、常に大文字と小文字を区別し、発音区別符号も区別します。で string 比較を実行する場合、構成された照合は無視されます。$replaceOne
たとえば、以下は照合強度 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é" } ] )
次の 操作は、"Cafe"$replaceOne の最初のインスタンスを検索して置き換えようとします。name フィールドの :
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é" }
$replaceOneはこのコレクションに構成された照合を無視するため、操作は"Cafe"2 のインスタンスにのみ一致します。ドキュメント の。
$match のように照合を尊重する演算子は、"Cafe" で文字列比較を実行すると、3 つのドキュメントすべてに一致することになります。これは、このコレクションの照合強度が 1 であるためです。
$replaceOne および Unicode 正規化
$replaceOne集計式はUnicode 正規化を実行しません。つまり、すべての 式の string マッチングでは、一致を試みるときに$replaceOne Unicode で文字を表すのに使用されるコード ポイントの数が考慮されます。
例、文字 é は、1 つのコード ポイントまたは 2 つのコード ポイントを使用して Unicode で表すことができます。
Unicode | 表示方法 | コード ポイント |
|---|---|---|
|
| 1 ( |
|
| 2 ( |
文字 が 1 つのコード点とともに $replaceOneUnicode で表される検索文字列で を使用しても、入力文字列にéé 2 つのコード ポイントを使用する のインスタンスと一致しません。
次の表は、検索文字列 éが一致するかどうかを示しています。入力文字列と比較した場合、 は 1 つのコード点または 2 つのコード ポイントで表されます。この例の検索文字列は、1 つのコード点を使用して 文字を表します。é
例 | 一致 |
|---|---|
| はい |
| no |
$replaceOneは 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" }, ] )
string を使用した置換
次の例えでは、 itemフィールド内の「青い塗料」の最初のインスタンスを「赤色の塗料」に置き換えます。
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" }
ドキュメント3では、「青い塗料」 の最初に一致したインスタンスのみが置き換えられることに注意してください。
正規表現を使用した置換
次の例では、itemフィールド内の単語全体としての「white」の最初のインスタンスを「natural」に置き換えます。
db.inventory.aggregate([ { $project: { item: { $replaceOne: { input: "$item", find: \\bblue\\b, replacement: "navy" } } } } ]);
この操作は次の結果を返します。
{ _id: 1, item: "navy paint" } { _id: 2, item: "navy and green paint" } { _id: 3, item: "navy paint with blue paintbrush" } { _id: 4, item: "navy paint with green paintbrush" }
Note that with document 3, only the first matched インスタンス of "blue" is replaced.