Definition定义
Note
Disambiguation消歧
$unsetRemoves/excludes fields from documents.从文档中删除/排除字段。
Syntax语法
The $unset stage has the following syntax:$unset阶段具有以下语法:
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:isbn和copies:
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 本页上的Node.js示例使用Atlas示例数据集中的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.sample_mflix数据库。要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门。
To use the MongoDB Node.js driver to add a 要使用MongoDB Node.js驱动程序向聚合管道添加$unset stage to an aggregation pipeline, use the $unset operator in a pipeline object.$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;