π Unset Fields from Results
$unsetβ
We can remove fields from the results, either using $project or more fine-grained, using $unset. If we don't want the attributes we can do:
- Atlas UI
- MongoDB Shell
[
{$unset: "attributes"}
]
db.books.aggregate([
{$unset: "attributes"}
])
π Remove from results the fields totalInventory
and available
.
Answer
- Atlas UI
- MongoDB Shell
Several ways to do this, other than using $project
:
[
{$unset: "totalInventory"},
{$unset: "available"},
]
// or
[
{$unset: ["totalInventory", "available"] },
]
db.books.aggregate([
{$unset: "totalInventory"},
{$unset: "available"},
])
// or
db.books.aggregate([
{$unset: ["totalInventory", "available"] },
])
info
$set is an alias for $addFields that you'll find on many older posts and documentation.