$unset (Remove Field)
Last updated: 2026-10-09
$unset (Remove Field)
$unset removes the specified field from a document. `{ $unset: { age: '' } }` — the value is a placeholder, often an empty string. Use dotted paths for nested fields; missing fields are silently ignored. Inside array elements $unset sets the value to null rather than removing the element (use $pull to remove).
| Category | Update Field |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $unset: { <field>: '' } }
⚙️ Parameters
| field | The field to remove; dotted paths allowed. |
|---|---|
| placeholder value | A placeholder value, usually an empty string. |
Returns:The update result
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $unset: remove the age field
db.users.updateOne(
{ _id: 1 },
{ $unset: { age: '' } }
)
// Remove a nested field
db.users.updateOne(
{ _id: 1 },
{ $unset: { 'address.city': '' } }
)
Output:
// The age and address.city fields are removed from the document
📌 Notes
- Inside array elements $unset sets null instead of removing
- The counterpart of $set: one removes, one writes
- Silently ignored when the field is missing
🏷️ Related Tags
💡 Related Cases
Schema Validation: Data Validation at the Database Layer
Document Update: A Detailed Explanation of `updateOne` and `updateMany`
📚 Related Tutorials
❓ FAQ
QDoes $unset remove array elements?
ANo. Inside arrays it sets the position to null; use $pull to remove elements.
QWhy must $unset carry a value?
AThe value is a placeholder, usually an empty string; any value behaves the same.
QCan $unset remove nested fields?
AYes, via dotted paths.