$unsetField (aggregation)
On this page本页内容
Definition定义
$unsetField
New in version 5.0.5.0版新增。Removes a specified field in a document.删除文档中的指定字段。You can use可以使用$unsetField
to remove fields with names that contain periods (.
) or that start with dollar signs ($
).$unsetField
删除名称中包含句点(.
)或以美元符号($
)开头的字段。$unsetField
is an alias for是使用$setField
using$$REMOVE
to remove fields.$$REMOVE
删除字段的$setField
的别名。
Syntax语法
$unsetField
has the following syntax:具有以下语法:
{
$unsetField: {
field: <String>,
input: <Object>,
}
}
You must provide the following fields:您必须提供以下字段:
field | String | input object that you want to add, update, or remove. input 对象中的字段。field can be any valid expression that resolves to a string constant.field 可以是解析为字符串常量的任何有效表达式。 |
input | Object | field that you want to add or update. field 的文档。input must resolve to an object, missing , null , or undefined .input 必须解析为对象、missing 、null 或undefined 。 |
Behavior行为
If如果input
evaluates tomissing
,undefined
, ornull
,$unsetField
returnsnull
and does not updateinput
.input
的计算结果为missing
、undefined
或null
,$unsetField
将返回null
并且不更新输入。If如果input
evaluates to anything other than an object,missing
,undefined
, ornull
,$unsetField
returns an error.input
的计算结果不是对象、missing
、undefined
或为null
,$unsetField
将返回错误。If如果field
resolves to anything other than a string constant,$unsetField
returns an error.field
解析为字符串常量以外的任何值,$unsetField
将返回一个错误。If若field
doesn't exist ininput
,$unsetField
adds it.input
中不存在field
,$unsetField
将添加该字段。$unsetField
doesn't implicitly traverse objects or arrays.不隐式遍历对象或数组。For example,例如,$unsetField
evaluates afield
value of"a.b.c"
as a top-level field"a.b.c"
instead of as a nested field,{ "a": { "b": { "c": } } }
.$unsetField
将field
值"a.b.c"
计算为顶级字段"a.b.c"
,而不是嵌套字段{ "a": { "b": { "c": } } }
。
Examples实例
Remove Fields that Contain Periods (.
)删除包含句点(.
)的字段
.
)Consider the inventory collection:考虑inventory
集合:
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:$replaceWith
管道阶段和$unsetField
运算符从每个文档中删除"price.usd"
字段:
db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: "price.usd",
input: "$$ROOT"
} } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _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 }
]
Remove Fields that Start with a Dollar Sign ($
)删除以美元符号($
)开头的字段
$
)Consider the inventory collection:考虑inventory
集合:
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:$unsetField
和$literal
运算符的$replaceWith
管道阶段从每个文档中删除"$price"
字段:
db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: { $literal: "$price" },
input: "$$ROOT"
} } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _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 }
]
Remove A Subfield删除子字段
Consider the inventory collection:考虑inventory
集合:
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 } }
] )
The "price"
field contains a document with two subfields, "usd"
and "euro"
. "price"
字段包含一个包含两个子字段的文档,即"usd"
和"euro"
。You cannot use 您不能使用"price.euro"
to identify and remove "euro"
because MongoDB parses "price.euro"
as a top level field name that happens to contain a period (.
)."price.euro"
来标识和删除"euro"
,因为MongoDB将"price.euro"
解析为恰好包含句点(.
)的顶级字段名称。
Use the 使用带有$replaceWith
pipeline stage with $setField
and a nested $unsetField
operation to remove the "euro"
field:$setField
的$replaceWith
管道阶段和嵌套的$unsetField
操作来删除"euro"
字段:
db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price",
input: "$$ROOT",
value: {
$unsetField: {
field: "euro",
input: { $getField: "price" }
} } } } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _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 } }
]