OPTIONS

$unset

$unset

The $unset operator deletes a particular field. The specified value in the $unset expression (i.e. "" below) does not impact the operation. If the field does not exist, then $unset has no effect. Consider the following syntax:

{ $unset: { <field1>: "", ... } }

For example, the following update() operation uses the $unset operator to remove the fields quantity and instock from the first document found in the products collection where the field sku has a value of unknown.

db.products.update( { sku: "unknown" },
                    { $unset: {
                                quantity: "",
                                instock: ""
                              }
                    }
                  )

To remove the fields from all documents in the collection where the field sku has a value of unknown, specify the multi: true option in the update() method, as in the following example:

db.products.update( { sku: "unknown" },
                    { $unset: {
                                quantity: "",
                                instock: ""
                              }
                    },
                    { multi: true }
                  )
←   $set $min  →
ON THIS PAGE