# updateOne & updateMany — MongoDB

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

> Modify existing documents that match a filter.

## updateOne()

Updates the first document that matches the filter, using an update operator like `$set`.

```javascript
db.users.updateOne(
  { name: "Grace Hopper" },
  { $set: { role: "admiral" } }
)
```

Output:

```
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
```

## updateMany()

Same idea, but applies to every matching document.

```javascript
db.users.updateMany(
  { role: "engineer" },
  { $set: { department: "platform" } }
)
```

Output:

```
{ acknowledged: true, matchedCount: 2, modifiedCount: 2 }
```

## Be specific with your filter

A too-broad filter in `updateMany()` can silently change documents you didn't mean to touch — always test the filter with `find()` first.
