- Reference >
- Operators >
- Update Operators >
- Field Update Operators >
- $set
$set¶
-
$set
¶ Syntax:
{ $set: { <field1>: <value1>, ... } }
Use the
$set
operator to replace the value of a field to the specified value. If the field does not exist, the$set
operator will add the field with the specified value.The following example uses the
$set
operator to update the value of thequantity
field to500
and theinstock
field totrue
for the first document where the fieldsku
has the valueabc123
:To update all matching documents in the collection, specify
multi: true
option in theupdate()
method, as in the following example which sets the value of the fieldinstock
totrue
for all documents in theproducts
collection where thequantity
field is greater than (i.e.$gt
)0
: