“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$unsetField(聚合)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例
$unsetField

版本 5.0 中的新增功能

删除文档中的指定字段。

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

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

$unsetField 通过以下语法实现:

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

您必须提供以下字段:

字段
类型
说明
field
字符串
您要添加、更新或删除的 input 对象中的字段。field 可以是解析为字符串常量的任何有效表达式
input
对象
一份文档,包含您要添加或更新的 fieldinputmissing 必须解析为对象、null、或 undefined
  • 如果 input 的计算结果为 missingundefinednull$unsetField 返会回 null,而不更新 input

  • 如果 input 的计算结果为对象、missingundefinednull 以外的任何值,$unsetField 将返回错误。

  • 如果 field 的解析结果不是字符串常量,$unsetField 将返回错误。

  • 如果 input 中不存在 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 } }
]

提示

另请参阅:

← $type(聚合)