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

$unsetField(演算子)

$unsetField

バージョン 5.0 の新機能。

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

You can use $unsetField to remove fields with names that contain periods (.) or that start with dollar signs ($).

$unsetField is an alias for $setField using $$REMOVE to remove fields.

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

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

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

フィールド
タイプ
説明

field

文字列

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

input

オブジェクト

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

  • If input evaluates to missing, undefined, or null, $unsetField returns null and does not update input.

  • If input evaluates to anything other than an object, missing, undefined, or null, $unsetField returns an error.

  • If field resolves to anything other than a string constant, $unsetField returns an error.

  • $unsetField doesn't implicitly traverse objects or arrays. For example, $unsetField evaluates a field value of "a.b.c" as a top-level field "a.b.c" instead of as a nested field, { "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 }
] )

Use the $replaceWith pipeline stage and the $unsetField operator to remove the "price.usd" field from each document:

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 }
] )

Use the $replaceWith pipeline stage with the $unsetField and $literal operators to remove the "$price" field from each document:

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" は、ピリオド(. )を含む最上位フィールド名として を解析するため、 を使用して を識別して削除することはできません。

Use the $replaceWith pipeline stage with $setField and a nested $unsetField operation to remove the "euro" field:

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 } }
]

$setField

このページを評価