$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.$setField使用$$REMOVE删除字段的别名。

Syntax语法

$unsetField has the following syntax:语法如下:

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

You must provide the following fields:您必须提供以下字段:

Field字段Type类型Description描述
fieldStringField in the input object that you want to add, update, or remove. 要添加、更新或删除的input对象中的字段。field can be any valid expression that resolves to a string constant.可以是解析为字符串常量的任何有效表达式
inputObjectDocument that contains the field that you want to add or update. 包含要添加或更新的field的文档。input must resolve to an object, missing, null, or undefined.必须解析为对象、missingnullundefined

Behavior行为

  • If input evaluates to missing, undefined, or null, $unsetField returns null and does not update input.如果input的计算结果为missingundefinednull$unsetField将返回null,并且不会更新输入。
  • If input evaluates to anything other than an object, missing, undefined, or null, $unsetField returns an error.如果input的计算结果不是对象、missingundefined或为null,则$unsetField将返回错误。
  • If field resolves to anything other than a string constant, $unsetField returns an error.field解析为字符串常量以外的任何值,$unsetField将返回错误。
  • If field doesn't exist in input, $unsetField adds it.如果input中不存在field,则$unsetField会添加它。
  • $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": } } }.例如,$unsetFieldfield"a.b.c"作为顶级字段"a.b.c"计算,而不是作为嵌套字段{ "a": { "b": { "c": } } }计算。

Examples示例

Remove Fields that Contain Periods (.)删除包含句点(.)的字段

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 }
]

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:$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 } }
]
Tip提示
See also: 参阅:
←  $type (aggregation)$week (aggregation) →