Navigation
This version of the documentation is archived and no longer supported.

$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 the quantity field to 500 and the instock field to true for the first document where the field sku has the value abc123:

db.products.update( { sku: "abc123" },
                    { $set: {
                              quantity: 500,
                              instock: true
                            }
                    }
                  )

To update all matching documents in the collection, specify multi: true option in the update() method, as in the following example which sets the value of the field instock to true for all documents in the products collection where the quantity field is greater than (i.e. $gt) 0 :

db.products.update( { quantity: { $gt: 0 } },
                    { $set: { instock: true } },
                    { multi: true }
                  )
←   $setOnInsert $unset  →