0
0
MongoDBquery~10 mins

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

Choose your learning style9 modes available
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.