Bird
Raised Fist0
MongoDBquery~10 mins

$unset operator for removing fields in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - $unset operator for removing fields
Start with a document
Apply $unset operator
Specify fields to remove
Remove specified fields
Return updated document without those fields
The $unset operator removes specified fields from a MongoDB document, returning the document without those fields.
Execution Sample
MongoDB
db.collection.updateOne(
  { _id: 1 },
  { $unset: { age: "", city: "" } }
)
This command removes the fields 'age' and 'city' from the document with _id 1.
Execution Table
StepDocument BeforeOperationFields to RemoveDocument After
1{"_id": 1, "name": "Alice", "age": 30, "city": "NY"}$unset{"age": "", "city": ""}{"_id": 1, "name": "Alice"}
2{"_id": 2, "name": "Bob", "age": 25}$unset{"city": ""}{"_id": 2, "name": "Bob", "age": 25}
3{"_id": 3, "name": "Carol", "city": "LA"}$unset{"city": ""}{"_id": 3, "name": "Carol"}
4No more documents to updateExitNo fieldsOperation complete
💡 No more matching documents or fields to remove
Variable Tracker
Document _idBefore $unsetAfter $unset
1{"_id": 1, "name": "Alice", "age": 30, "city": "NY"}{"_id": 1, "name": "Alice"}
2{"_id": 2, "name": "Bob", "age": 25}{"_id": 2, "name": "Bob", "age": 25}
3{"_id": 3, "name": "Carol", "city": "LA"}{"_id": 3, "name": "Carol"}
Key Moments - 3 Insights
Why does the document with _id 2 not change after $unset?
Because the $unset tried to remove 'city' which does not exist in that document, so no change happened (see execution_table row 2).
What happens if you try to $unset a field that is not present?
MongoDB simply ignores it and leaves the document unchanged, as shown in execution_table row 2.
Can $unset remove multiple fields at once?
Yes, you can specify multiple fields in the $unset object, and all will be removed if present (see execution_table row 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the document after $unset at step 3?
A{"_id": 3, "name": "Carol", "city": "LA"}
B{"_id": 3, "name": "Carol"}
C{"_id": 3, "city": "LA"}
D{"_id": 3}
💡 Hint
Check the 'Document After' column at step 3 in the execution_table.
At which step does the $unset operator remove both 'age' and 'city' fields?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Fields to Remove' column and see which step lists both fields.
If you try to $unset a field not present in the document, what happens?
AThe document remains unchanged
BThe document is deleted
CAn error is thrown
DAll fields are removed
💡 Hint
Refer to step 2 in the execution_table where 'city' was not present.
Concept Snapshot
$unset operator removes specified fields from a MongoDB document.
Syntax: { $unset: { field1: "", field2: "" } }
Only fields listed are removed; others stay unchanged.
If a field does not exist, no error occurs.
Useful for cleaning up documents by deleting unwanted fields.
Full Transcript
The $unset operator in MongoDB is used to remove fields from documents. When you apply $unset with a list of fields, MongoDB deletes those fields if they exist in the document. If a field is not present, MongoDB ignores it without error. For example, if a document has fields 'age' and 'city', using $unset on these fields removes them, leaving the rest of the document intact. This operation is useful to clean documents by deleting unwanted fields. The execution table shows step-by-step how documents change after applying $unset, including cases where fields are missing and no change occurs.

Practice

(1/5)
1. What does the $unset operator do in MongoDB?
easy
A. Updates the value of existing fields
B. Adds new fields to documents
C. Removes specified fields from documents
D. Duplicates documents in a collection

Solution

  1. Step 1: Understand the purpose of $unset

    The $unset operator is used to remove fields from documents in MongoDB.
  2. Step 2: Compare with other operators

    Unlike $set which adds or updates fields, $unset deletes them.
  3. Final Answer:

    Removes specified fields from documents -> Option C
  4. Quick Check:

    $unset removes fields [OK]
Hint: Remember: $unset deletes fields, $set adds or updates [OK]
Common Mistakes:
  • Confusing $unset with $set
  • Thinking $unset adds fields
  • Assuming $unset duplicates documents
2. Which of the following is the correct syntax to remove the field age from all documents in a collection named users?
easy
A. db.users.updateMany({}, {$unset: {age: null}})
B. db.users.updateMany({}, {$unset: {age: true}})
C. db.users.updateMany({}, {$unset: {age: ""}})
D. db.users.updateMany({}, {$unset: {age: 1}})

Solution

  1. Step 1: Recall correct $unset syntax

    The $unset operator requires the field name with a value of 1 to remove it.
  2. Step 2: Check each option

    Only db.users.updateMany({}, {$unset: {age: 1}}) uses age: 1, which is the correct way to remove the field.
  3. Final Answer:

    db.users.updateMany({}, {$unset: {age: 1}}) -> Option D
  4. Quick Check:

    Use 1 as value with $unset [OK]
Hint: Use 1 as value to remove fields with $unset [OK]
Common Mistakes:
  • Using true, null, or empty string instead of 1
  • Forgetting curly braces around field
  • Using $set instead of $unset
3. Given the collection products with documents:
{ "name": "Pen", "price": 5, "color": "blue" }
{ "name": "Notebook", "price": 10, "color": "red" }

What will be the result of this update?
db.products.updateMany({}, { $unset: { color: 1 } })

What will the documents look like after the update?
medium
A. [{ "name": "Pen", "price": 5, "color": "blue" }, { "name": "Notebook", "price": 10, "color": "red" }]
B. [{ "name": "Pen", "price": 5 }, { "name": "Notebook", "price": 10 }]
C. [{ "name": "Pen", "color": "blue" }, { "name": "Notebook", "color": "red" }]
D. [{ "price": 5, "color": "blue" }, { "price": 10, "color": "red" }]

Solution

  1. Step 1: Understand $unset effect

    The $unset operator removes the specified field from all documents.
  2. Step 2: Apply $unset to color field

    Removing color means it will no longer appear in any document, leaving only name and price.
  3. Final Answer:

    [{ "name": "Pen", "price": 5 }, { "name": "Notebook", "price": 10 }] -> Option B
  4. Quick Check:

    Fields removed by $unset disappear from documents [OK]
Hint: Fields removed by $unset vanish from all matched documents [OK]
Common Mistakes:
  • Expecting fields to be set to null instead of removed
  • Thinking $unset changes field values
  • Assuming only one document is affected
4. You run this command:
db.orders.updateMany({}, { $unset: { "status" } })

But it throws an error. What is the problem?
medium
A. The field name must be a string key with a value, not just a key
B. You cannot use $unset with updateMany
C. The collection name is incorrect
D. The $unset operator requires an array of fields

Solution

  1. Step 1: Check $unset syntax

    The $unset operator requires field names as keys with a value (usually 1) to specify removal.
  2. Step 2: Identify missing value

    In the command, { $unset: { "status" } } is invalid because the field "status" has no value.
  3. Final Answer:

    The field name must be a string key with a value, not just a key -> Option A
  4. Quick Check:

    $unset needs field: value pairs [OK]
Hint: Always provide a value (like 1) with field names in $unset [OK]
Common Mistakes:
  • Omitting the value for the field in $unset
  • Using $unset with wrong update method
  • Assuming $unset accepts arrays
5. You have documents in employees collection with fields name, age, department, and tempField. You want to remove tempField only from employees in the Sales department without affecting others. Which command achieves this?
hard
A. db.employees.updateMany({ department: "Sales" }, { $unset: { tempField: 1 } })
B. db.employees.updateMany({}, { $unset: { tempField: 1 } })
C. db.employees.updateMany({ tempField: { $exists: true } }, { $unset: { department: 1 } })
D. db.employees.updateMany({ department: "Sales" }, { $set: { tempField: null } })

Solution

  1. Step 1: Filter documents by department

    Use the query { department: "Sales" } to target only Sales employees.
  2. Step 2: Use $unset to remove tempField

    Apply $unset: { tempField: 1 } to remove the field from matched documents only.
  3. Final Answer:

    db.employees.updateMany({ department: "Sales" }, { $unset: { tempField: 1 } }) -> Option A
  4. Quick Check:

    Filter then $unset removes fields selectively [OK]
Hint: Filter first, then $unset fields to remove selectively [OK]
Common Mistakes:
  • Removing fields from all documents unintentionally
  • Using $set to null instead of $unset
  • Removing wrong fields by mistake