$unset is a MongoDB aggregation stage that removes one or more fields from every document in the pipeline. Everything else stays—so it is the easy way to drop passwords, temps, or nested paths before you return results.
Remember
$unset = drop these fields, keep the rest
{ $unset: "password" }
{ $unset: [ "password", "tempScore" ] }
nested → "profile.temp"
missing field → no error
vs $project → allow-list / reshape
vs $set → add/overwrite fields
pipeline only → collection unchanged
Name the fields to drop, then place the stage where you want them gone.
List the field names as a string or an array of strings.
Use dot notation for nested paths (for example "profile.temp").
Put $unset after you finish using those fields (often after $set / $sort).
Remember: this cleans the pipeline output, not the stored collection.
Piece
Required?
Role
Field string
One form
Drop a single field
Array of strings
One form
Drop several fields at once
Dot path
No
Remove a nested field
Pseudocode
Pseudocode
fieldsToDrop = ["password", "tempScore"]
for each document:
for each name in fieldsToDrop:
delete document[name] // or nested path
emit document // other fields unchanged
Reference
Cheat Sheet
Keep these facts handy while cleaning pipeline documents.
Topic
Detail
Type
Aggregation stage
Syntax
{ $unset: "field" } or [ ... ]
Keeps
All fields not listed
Nested
Dot notation
Missing field
Ignored (safe)
vs $project
Drop-list vs keep-list
Sample
Drop password, keep name
Try it
Live Preview
Start from a student document and drop fields the way $unset would.
Simulates { $unset: ... } on a sample student document.
Result
$unset: "password"
before → { name, email, password, tempScore, profile }
after → { name, email, tempScore, profile }
Trace
Worked Walkthrough — Hide Password
Asha’s document includes a password. Before we return her profile, we drop that field with $unset.
We use one student document. First insert it, then remove password, then remove several fields at once. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine student Asha with an email, a password, a temporary score, and a nested profile.temp flag. We save her in students.
Pass an array. Each string is a field (or nested path) to remove.
Dot notation."profile.temp" removes only that nested key.
Clean response. What remains is safe public profile data.
Edge Cases & Pitfalls
Check these before stripping fields in production pipelines.
storage
Does not update the collection
$unset only changes the pipeline stream. Persist with $out / $merge (or an update) if you need data removed on disk.
order
Unset after you use the field
If a later stage needs tempScore, do not drop it first. Compute → use → then $unset.
project
Many drops vs few keeps
Dropping lots of fields? An allow-list with $project is often clearer than a huge $unset list.
nested
Dot path vs whole object
"profile.temp" removes one nested key. "profile" removes the entire object.
Analysis
Performance Notes
Approach
Cost
Best for
$unset few fields
Cheap reshape
Secrets / temps
$project allow-list
Cheap reshape
Slim API responses
Unset very late
Carries fields longer
OK if needed for middle stages
Dropping fields is inexpensive. Prefer removing large unused fields before heavy stages when documents are wide and the pipeline is long.
Remember
Key Takeaways
Role: remove named fields; keep the rest.
Syntax: string or array of field paths.
vs $project: drop-list vs keep-list.
Sample: drop password; keep name / email.
One line:$unset answers “remove these fields from every pipeline document.”
Frequently Asked Questions
$unset removes one or more fields from every document in the aggregation pipeline. All other fields stay. Use it to drop passwords, temps, or nested paths before returning results.
Use { $unset: "field" } for one field, or { $unset: [ "field1", "field2" ] } for several. Nested paths use dot notation, such as "profile.temp".
$unset drops named fields and keeps the rest. $project reshapes output—you usually list fields to keep. Prefer $unset for removing a few known fields; prefer $project for a slim allow-list.
Yes. $set / $addFields add or overwrite fields; $unset removes them. A common pattern is compute a helper with $set, use it, then $unset it before the client sees the document.
No. It only affects documents in the pipeline stream unless you write results back with $out or $merge.
Nothing bad happens. MongoDB simply leaves the document as-is for that missing field.