Database Manual / Reference / Query Language / Aggregation Stages

$unset (aggregation stage)(聚合阶段)

Definition定义

Note

Disambiguation消歧

The following page refers to the aggregation stage $unset. For the update operator $unset, see $unset.以下页面涉及聚合阶段$unset。关于更新运算符$unset,请参阅$unset

$unset
Removes/excludes fields from documents.从文档中删除/排除字段。

Syntax语法

The $unset stage has the following syntax:$unset阶段具有以下语法:

  • To remove a single field, the $unset takes a string that specifies the field to remove:要删除单个字段,$unset需要一个指定要删除的字段的字符串:

    { $unset: "<field>" }
  • To remove multiple fields, the $unset takes an array of fields to remove.要删除多个字段,$unset需要删除一个字段数组。

    { $unset: [ "<field1>", "<field2>", ... ] }

Considerations注意事项

$unset and $project

The $unset is an alias for the $project stage that removes/excludes fields:$unset是删除/排除字段的$project阶段的别名:

{ $project: { "<field1>": 0, "<field2>": 0, ... } }

Embedded Fields嵌入式字段

To remove/exclude a field or fields within an embedded document, you can use the dot notation, as in:要删除/排除嵌入式文档中的一个或多个字段,可以使用点符号,如:

{ $unset: "<field.nestedfield>" }

or

{ $unset: [ "<field1.nestedfield>", ...] }

Examples示例

MongoDB Shell

Create a sample books collection with the following documents:使用以下文档创建示例books集合:

db.books.insertMany([
{ "_id" : 1, title: "Antelope Antics", isbn: "0001122223334", author: { last:"An", first: "Auntie" }, copies: [ { warehouse: "A", qty: 5 }, { warehouse: "B", qty: 15 } ] },
{ "_id" : 2, title: "Bees Babble", isbn: "999999999333", author: { last:"Bumble", first: "Bee" }, copies: [ { warehouse: "A", qty: 2 }, { warehouse: "B", qty: 5 } ] }
])

Remove a Single Field删除单个字段

The following example removes the top-level field copies:以下示例删除顶级字段copies

db.books.aggregate([ { $unset: "copies" } ])

Alternatively, you can also use the following syntax:或者,您也可以使用以下语法:

db.books.aggregate([ { $unset: [ "copies" ] } ])

Either operation returns the following documents:任一操作都会返回以下文档:

{ "_id" : 1, "title" : "Antelope Antics", "isbn" : "0001122223334", "author" : { "last" : "An", "first" : "Auntie" } }
{ "_id" : 2, "title" : "Bees Babble", "isbn" : "999999999333", "author" : { "last" : "Bumble", "first" : "Bee" } }

Remove Top-Level Fields删除顶级字段

The following example removes the top-level fields isbn and copies:以下示例删除了顶级字段isbncopies

db.books.aggregate([
{ $unset: [ "isbn", "copies" ] }
])

The $unset operation outputs the following documents:$unset操作输出以下文档:

{ "_id" : 1, "title" : "Antelope Antics", "author" : { "last" : "An", "first" : "Auntie" } }
{ "_id" : 2, "title" : "Bees Babble", "author" : { "last" : "Bumble", "first" : "Bee" } }

Remove Embedded Fields删除嵌入字段

The following example removes the top-level field isbn, the embedded field first (from the name document) and the embedded field warehouse (from the elements in the copies array):以下示例删除了顶级字段isbn、嵌入字段first(从名称文档中)和嵌入字段warehouse(从副本数组中的元素中):

db.books.aggregate([
{ $unset: [ "isbn", "author.first", "copies.warehouse" ] }
])

The $unset operation outputs the following documents:$unset操作输出以下文档:

{ "_id" : 1, "title" : "Antelope Antics", "author" : { "last" : "An" }, "copies" : [ { "qty" : 5 }, { "qty" : 15 } ] }
{ "_id" : 2, "title" : "Bees Babble", "author" : { "last" : "Bumble" }, "copies" : [ { "qty" : 2 }, { "qty" : 5 } ] }
Node.js

The Node.js examples on this page use the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB Node.js driver documentation.本页上的Node.js示例使用Atlas示例数据集中的sample_mflix数据库。要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门

To use the MongoDB Node.js driver to add a $unset stage to an aggregation pipeline, use the $unset operator in a pipeline object.要使用MongoDB Node.js驱动程序向聚合管道添加$unset阶段,请在管道对象中使用$unset运算符。

The following example creates a pipeline stage that excludes the tomatoes field and the imdb.votes embedded field from return documents. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,该阶段从返回文档中排除了tomatoes字段和imdb.votes嵌入字段。然后,该示例运行聚合管道:

const pipeline = [{ $unset: ["imdb.votes", "tomatoes"] }];

const cursor = collection.aggregate(pipeline);
return cursor;