- Reference >
- Operators >
- Update Operators >
- Field Update Operators >
- $set
$set¶
-
$set¶ Syntax:
{ $set: { <field1>: <value1>, ... } }Use the
$setoperator to replace the value of a field to the specified value. If the field does not exist, the$setoperator will add the field with the specified value.The following example uses the
$setoperator to update the value of thequantityfield to500and theinstockfield totruefor the first document where the fieldskuhas the valueabc123:To update all matching documents in the collection, specify
multi: trueoption in theupdate()method, as in the following example which sets the value of the fieldinstocktotruefor all documents in theproductscollection where thequantityfield is greater than (i.e.$gt)0: