$unsetField(聚合)
定义
$unsetField
新版本5.0.
从文档中删除指定的字段。
您可以使用
$unsetField
来删除包含点(.
)或以美元符号($
)开头的字段名。$unsetField
是$setField
的别名,使用$$REMOVE
来删除字段。
语法
$unsetField
具有以下语法
{ $unsetField: { field: <String>, input: <Object>, } }
必须提供以下字段
字段 | 类型 | 描述 |
---|---|---|
field | String | |
input | Object | 包含要删除的 field 的文档。 input 必须解析为对象、missing 、null 或 undefined 。 |
行为
如果
input
评估为缺失
、未定义
或null
,则$unsetField
返回null
并不更新input
。如果
input
评估为除对象以外的任何内容,则$unsetField
返回错误。如果
field
解析为除字符串常量以外的任何内容,则$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”字段包含一个文档,其中有两个子字段,“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 } } ]