The update operator in MongoDB is used to modify the values of specified fields in documents. The basic syntax of the update operator is as follows:
```
db.collection.update(
)
```
- `
- `
- The optional parameters like `upsert`, `multi` etc. further control the operations.
For example, if you have a collection called `students` and you wish to update the student document where `name` is `Alex`, you would do the following:
```
db.students.update(
{ “name” : “Alex” },
{ $set: { “grade” : “A” } }
)
```
The `$set` operator replaces the value of a field with the specified value.
`upsert` option:
If set to true, this will create a new document when no document matches the query criteria.
`multi` option:
If set to true, this updates multiple documents that meet the query criteria. If set to false, it updates one document.
Note: As of MongoDB 4.2, the `db.collection.update()` method can update only a single document. For multiple documents updates, use `db.collection.updateMany()`.
In MongoDB 4.2 and earlier, you need to use the `multi` option to update multiple documents.
Documentation: https://docs.mongodb.com/manual/reference/method/db.collection.update/