Definition
Note
Disambiguation
Syntax
The $unset stage has the following syntax:
Considerations
$unset and $project
The $unset is an alias for the $project
stage that removes/excludes fields:
{ $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
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.
To use the MongoDB Node.js driver to add a $unset stage to an aggregation
pipeline, use the $unset operator in a pipeline object.
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:
const pipeline = [{ $unset: ["imdb.votes", "tomatoes"] }]; const cursor = collection.aggregate(pipeline); return cursor;