对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$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可以是解析为字符串常量的任何有效表达式。

input

对象

包含要删除的 field 的文档。 input 必须解析为对象、missing、null 或 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" 字段包含一个具有两个子字段的文档,即 "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

给本页内容打分