MongoDB $unset Stage

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $unset?

$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
UsePrefer
Drop a few known fields$unset
Keep only an allow-list$project
Add / overwrite fields$set / $addFields

How to Use $unset in MongoDB

Name the fields to drop, then place the stage where you want them gone.

  1. List the field names as a string or an array of strings.
  2. Use dot notation for nested paths (for example "profile.temp").
  3. Put $unset after you finish using those fields (often after $set / $sort).
  4. Remember: this cleans the pipeline output, not the stored collection.
PieceRequired?Role
Field stringOne formDrop a single field
Array of stringsOne formDrop several fields at once
Dot pathNoRemove 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

Cheat Sheet

Keep these facts handy while cleaning pipeline documents.

TopicDetail
TypeAggregation stage
Syntax{ $unset: "field" } or [ ... ]
KeepsAll fields not listed
NestedDot notation
Missing fieldIgnored (safe)
vs $projectDrop-list vs keep-list
SampleDrop password, keep name

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 }

Worked Walkthrough — Hide Password

Asha’s document includes a password. Before we return her profile, we drop that field with $unset.

Calculation
Document:
  {
    name: "Asha",
    email: "[email protected]",
    password: "secret",
    tempScore: 88
  }

$unset: "password"

Result:
  {
    name: "Asha",
    email: "[email protected]",
    tempScore: 88
  }

password gone · other fields kept

MongoDB Examples

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.

mongosh
db.students.insertOne({
  _id: 1,
  name: "Asha",
  email: "[email protected]",
  password: "secret",
  tempScore: 88,
  profile: { city: "Chennai", temp: true }
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in students. Run find() to see it:

mongosh
db.students.find()

Read that document like this:

FieldKeep?Plain English
name / emailYesSafe to return
passwordNoMust drop before clients
tempScoreMaybeHelper field for the pipeline
profile.tempMaybeNested flag to clean later
Picture
{ name, email, password, tempScore, profile }
                 ↓ $unset "password"
{ name, email, tempScore, profile }

How It Works

  1. Insert once. insertOne adds Asha’s document to students.
  2. Sensitive field present. password is what we will drop next.
  3. find() shows the full shape. Cleaning happens in the pipeline, not in storage.

Example 2 — Unset One Field

Goal: remove password from the pipeline output.

mongosh
db.students.aggregate([
  { $unset: "password" }
]);

How It Works

  1. Name the field. "password" is the only drop target.
  2. Keep the rest. name, email, and others still appear.
  3. Collection unchanged. A later find() on students still has password.

Example 3 — Unset Several Fields

Same student, but drop password, tempScore, and the nested profile.temp flag in one stage.

mongosh
db.students.aggregate([
  {
    $unset: ["password", "tempScore", "profile.temp"]
  }
]);

How It Works

  1. Pass an array. Each string is a field (or nested path) to remove.
  2. Dot notation. "profile.temp" removes only that nested key.
  3. 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.

Performance Notes

ApproachCostBest for
$unset few fieldsCheap reshapeSecrets / temps
$project allow-listCheap reshapeSlim API responses
Unset very lateCarries fields longerOK 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.

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.

Next: $unwind

Turn each array element into its own pipeline document.

$unwind tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful