文档菜单
文档首页
/
MongoDB 手册
/ / /

$unsetField(聚合)

本页内容

  • 定义
  • 语法
  • 行为
  • 示例
  • 了解更多
$unsetField

版本5.0.

从文档中删除指定的字段。

您可以使用$unsetField 来删除包含点(.)或以美元符号($)开头的字段名。

$unsetField$setField 的别名,使用 $$REMOVE 来删除字段。

$unsetField 具有以下语法

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

必须提供以下字段

字段
类型
描述
field
String
要删除的 input 对象中的字段。 field 可以是任何有效的 表达式,其解析结果为字符串常量。
input
Object
包含要删除的 field 的文档。 input 必须解析为对象、missingnullundefined
  • 如果 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 } }
]

$setField

返回

$type