AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$replaceOne(演算子)

$replaceOne

input string内の検索stringまたは正規表現パターンの最初のインスタンスを置換stringで置き換えます。

If no occurrences are found, $replaceOne evaluates to the input string.

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

The $replaceOne operator has the following operator expression syntax:

{ $replaceOne: { 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 null. If input refers to a field that is missing, $replaceOne returns null.

The string to search for within the given input. Can be any valid expression that resolves to a string, a regex, or a null. If find refers to a missing field, $replaceOne returns null.

The string to use to replace the first matched instance of find in input. Can be any valid expression that resolves to a string or a null.

If no occurrences of find are found in input, $replaceOne evaluates to the input string.

The input, find, and replacement expressions must evaluate to one of the following output types:

  • A string

  • null

  • A regex for find operations

Otherwise, $replaceOne fails with an error.

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 $replaceOne expression evaluates to 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 式の 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é" }
] )

The following $replaceOne operation tries to find and replace the first instance of "Cafe" in the name field:

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 $replaceOne ignores the collation configured for this collection, the operation only matches the instance of "Cafe" in document 2.

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

The $replaceOne aggregation expression does not perform any unicode normalization. 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.

例、文字 é は、1 つのコード ポイントまたは 2 つのコード ポイントを使用して 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.

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:

一致

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

はい

{ $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 é.

次のドキュメントを使用して 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" },
] )

次の例えでは、 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.

このページを評価