Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

$unsetField(集計)

項目一覧

  • 定義
  • 構文
  • 動作
$unsetField

バージョン 5.0 で追加

ドキュメント内の指定したフィールドを削除します。

$unsetFieldを使用すると、名前にピリオド( . )が含まれる名前付きフィールド、またはドル記号( $ )で始まる名前付きフィールドを削除できます。

$unsetFieldは、 $$REMOVEを使用してフィールドを削除する $setFieldのエイリアスです。

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

{
$unsetField: {
field: <String>,
input: <Object>,
}
}

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

フィールド
タイプ
説明
field
文字列
追加、更新、または削除するinputオブジェクト内のフィールド。 fieldは、string 定数に変換される任意の有効なにすることができます。
input
オブジェクト
追加または更新するfieldを含むドキュメント。 inputは、オブジェクトmissingnull 、またはundefinedに変換される必要があります。
  • inputmissingundefined 、またはnullと評価された場合、 $unsetFieldnullを返し、 inputを更新しません。

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

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

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

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

インベントリ コレクションを検討します。

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パイプライン ステージと$unsetField演算子を使用して、各ドキュメントから"price.usd"フィールドを削除します。

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

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

[
{ _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 }
]

インベントリ コレクションを検討します。

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演算子と 演算子とともに パイプライン ステージを使用して、各ドキュメントから$unsetField $literalフィールドを削除します。"$price"

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

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

[
{ _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 }
]

インベントリ コレクションを検討します。

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

"price"フィールドには、2 つのサブフィールド"usd""euro"を持つドキュメントが含まれています。 "price.euro""euro"MongoDB"price.euro" は、ピリオド(. )を含む最上位フィールド名として を解析するため、 を使用して を識別して削除することはできません。

とネストされた $replaceWith$setField$unsetField操作とともに パイプライン ステージを使用して、 フィールドを削除します。"euro"

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price",
input: "$$ROOT",
value: {
$unsetField: {
field: "euro",
input: { $getField: "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 } }
]

Tip

以下も参照してください。

戻る

$type