# Update Operators — MongoDB

Source: https://www.geekswithgeeks.com/en/mongodb/mongo-update-operators

> $set, $inc, and $push — the everyday toolkit.

## The common operators

`$set` sets a field's value, `$inc` increments a number, `$push` appends to an array, `$unset` removes a field.

## $inc

Increments (or decrements with a negative value) a numeric field atomically.

```javascript
db.products.updateOne(
  { name: "Keyboard" },
  { $inc: { stock: -1 } }
)
```

## $push

Appends a new value to an array field.

```javascript
db.posts.updateOne(
  { _id: postId },
  { $push: { comments: "Great article!" } }
)
```

**Quiz:** Which operator increments a numeric field?

- [ ] $set
- [ ] $push
- [x] $inc

*Answer:* $inc. $inc atomically adds (or subtracts) a value from a number field.
