Definition
Note
Disambiguation
$unsetThe
$unsetoperator deletes a particular field.
Compatibility
You can use $unset for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
Consider the following syntax:
{ $unset: { <field1>: "", ... } }
The specified value in the $unset expression (i.e. "")
does not impact the operation.
To specify a <field> in an embedded document or in an array, use
dot notation.
Behavior
In MongoDB 4.4 and earlier, update operators process document fields in lexicographic order. See Update Operators Behavior for details.
If the field does not exist, then $unset does nothing (i.e.
no operation).
When used with $ to match an array element, $unset
replaces the matching element with null rather than removing the
matching element from the array. This behavior keeps consistent the
array size and element positions.
Example
The following update() operation uses the
$unset operator to remove the fields quantity and
instock from the first document in the products collection
where the field sku has a value of unknown.
db.products.update( { sku: "unknown" }, { $unset: { quantity: "", instock: "" } } )