On this page本页内容
$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.$setField使用$$REMOVE删除字段的别名。
$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 |
input | Object | field that you want to add or update. field的文档。inputmissing, null, or undefined.missing、null或undefined。
|
input evaluates to missing, undefined, or null, $unsetField returns null and does not update input.input的计算结果为missing、undefined或null,$unsetField将返回null,并且不会更新输入。input evaluates to anything other than an object, missing, undefined, or null, $unsetField returns an error.input的计算结果不是对象、missing、undefined或为null,则$unsetField将返回错误。field resolves to anything other than a string constant, $unsetField returns an error.field解析为字符串常量以外的任何值,$unsetField将返回错误。field doesn't exist in input, $unsetField adds it.input中不存在field,则$unsetField会添加它。$unsetField$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": } } }.$unsetField将field值"a.b.c"作为顶级字段"a.b.c"计算,而不是作为嵌套字段{ "a": { "b": { "c": } } }计算。.).)的字段Consider the inventory collection:考虑库存集合:
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 }
]
$)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 }
]
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:$replaceWith管道阶段与$setField和嵌套的$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 } }
]