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

$setField(演算子)

$setField

バージョン 5.0 の新機能。

ドキュメント内の指定フィールドを追加、アップデート、排除します。

$setFieldを使用して、名前にピリオド( )が含まれるフィールドやドル記号( )で始まるフィールドを追加、更新、または削除できます。.$

ルートレベルのドルプレフィックスのフィールド名は、一部のクエリプロセシングコンテキストで削除されるか、アクセスできない場合があるため、予約された内部メタデータフィールド名と衝突するものは使用しないでください。ドルフィルドの名前にドル記号$ () やピリオド. () が含まれる場合の制限とガイダンスについては、「 ピリオドとドル記号を含むフィールド名」を参照してください。

Tip

$setFieldを使用して、 $getFieldで追加または更新したドル記号($ )またはピリオド(. )を含むフィールドの値を取得します。

$setField の構文は次のとおりです。

{
$setField: {
field: <String>,
input: <Object>,
value: <Expression>
}
}

次のフィールドを提供する必要があります。

フィールド
タイプ
説明

field

文字列

追加、更新、または削除するinputオブジェクト内のフィールド。 fieldは、string 定数に変換される任意の有効なにすることができます。

input

オブジェクト

追加または更新するfieldを含むドキュメント。 inputは、オブジェクトmissingnull 、またはundefinedに変換される必要があります。

value

fieldに割り当てる値。 valueは任意の有効な式 にすることができます。

$$REMOVEに設定すると、 inputドキュメントからfieldが削除されます。

  • inputmissing、 、またはundefinednull と評価された場合、 は$setField を返し、null inputを更新しません。

  • inputがオブジェクト、missingundefined 、または 以外と評価された場合、null $setFieldはエラーを返します。

  • field string 定数以外の値に解決される場合、$setField はエラーを返します。

  • fieldinput に存在しない場合、$setField はそれを追加します。

  • $setFieldは、オブジェクトまたは配列を暗黙的にトラバースすることはありません。例、$setField fieldは、 の 値を、ネストされたフィールド、"a.b.c" としてではなく、最上位フィールド"a.b.c" { "a": { "b": { "c": } } }として評価します。

  • $unsetField$setField の入力値を持つ$$REMOVE のエイリアスです。次の式は同等です。

    {
    $setField: {
    field: <field name>,
    input: "$$ROOT",
    value: "$$REMOVE"
    }
    }
    {
    $unsetField: {
    field: <field name>,
    input: "$$ROOT"
    }
    }

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", price: 45.99, qty: 300 },
{ _id: 2, item: "winter coat", price: 499.99, qty: 200 },
{ _id: 3, item: "sun dress", price: 199.99, qty: 250 },
{ _id: 4, item: "leather boots", price: 249.99, qty: 300 },
{ _id: 5, item: "bow tie", price: 9.99, qty: 180 }
] )

次の操作では、$replaceWith パイプラインステージと$setField 演算子を使用して、各ドキュメントに新しいフィールド"price.usd" を追加します。 の値は、各ドキュメントの"price.usd" "price"の値と等しくなります。最後に、この操作では$unset パイプラインステージを使用して"price" フィールドを削除します。

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )

この操作は次の結果を返します。

[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
]

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", price: 45.99, qty: 300 },
{ _id: 2, item: "winter coat", price: 499.99, qty: 200 },
{ _id: 3, item: "sun dress", price: 199.99, qty: 250 },
{ _id: 4, item: "leather boots", price: 249.99, qty: 300 },
{ _id: 5, item: "bow tie", price: 9.99, qty: 180 }
] )

次の操作では、$replaceWith パイプラインステージと$setField および$literal 演算子を使用して、各ドキュメントに新しいフィールド"$price" を追加します。 の値は、各ドキュメントの"$price" "price"の値と等しくなります。最後に、この操作では$unset パイプラインステージを使用して"price" フィールドを削除します。

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )

この操作は次の結果を返します。

[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
]

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )

次の操作では、$match パイプラインステージを使用して特定のドキュメントを検索し、$replaceWith パイプラインステージと$setField 演算子を使用して一致するドキュメントの"price.usd" フィールドをアップデートします。

db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: 49.99
} } }
] )

この操作は次の結果を返します。

[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 49.99 }
]

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany([
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
] )

次の操作では、$match パイプラインステージを使用して特定のドキュメントを検索し、$replaceWith パイプラインステージと 演算子と$setField $literal演算子を使用して一致するドキュメントの"$price" フィールドをアップデートします。

db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: 49.99
} } }
] )

この操作は次の結果を返します。

[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 49.99 }
]

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany([
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )

次の操作では、 パイプラインステージと$replaceWith 演算子と$setField $$REMOVE"price.usd"を使用して、各ドキュメントから フィールドを削除します。

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )

この操作は次の結果を返します。

[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]

$unsetFieldエイリアスを使用して記述された同様のクエリでは、同じ結果が返されます。

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: "price.usd",
input: "$$ROOT"
} } }
] )

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
] )

次の操作では、$replaceWith パイプラインステージ、$setField 演算子と$literal 演算子、および$$REMOVE "$price"を使用して、各ドキュメントから フィールドを削除します。

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )

この操作は次の結果を返します。

[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]

$unsetFieldエイリアスを使用して記述された同様のクエリでは、同じ結果が返されます。

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: { $literal: "$price" },
input: "$$ROOT"
} } }
] )
このページを評価