定義
$setFieldバージョン 5.0 で追加
ドキュメント内の指定フィールドを追加、アップデート、排除します。
$setFieldを使用して、名前にピリオド(.)が含まれるフィールドやドル記号($)で始まるフィールドを追加、更新、または削除できます。
構文
$setField の構文は次のとおりです。
{ $setField: { field: <String>, input: <Object>, value: <Expression> } }
次のフィールドを提供する必要があります。
フィールド | タイプ | 説明 |
|---|---|---|
| 文字列 | 追加、更新、または削除する |
| オブジェクト | 追加または更新する |
| 式 |
を |
動作
inputがmissing、undefined、またはnullと評価された場合、$setFieldはnullを返し、inputを更新しません。inputがオブジェクト以外のオブジェクト(missing、undefined、またはnull)以外と評価された場合、$setFieldはエラーを返します。fieldが string 定数以外に解決される場合、$setFieldはエラーを返します。fieldがinputに存在しない場合、$setFieldはそれを追加します。$setFieldはオブジェクトまたは配列を暗黙的にトラバースすることはありません。 たとえば、$setFieldは、"a.b.c"のfield値を、ネストされたフィールド{ "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" } } } ] )