| |
read source history
= update() =
----
[[MongoDB Driver Function Reference]]
Updates all documents in a collection that meet the filter criteria.
This method is identically equal to [[mongodrv-col-updateMany|updateMany()]]. This is specifically different to the behavior of the [[https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/index.html|MongoDB Shell method]], that updates a single document by default.
[[[@cG
<collection>.update(<filter>, <update>[, <options>])
]]]
**Arguments**
[[[@w
|@t0@r0@c0 <collection> | | The target collection. |
| <filter> | | A filter expression using [[https://docs.mongodb.com/manual/reference/operator/|query operators]]. |
| <update> | | A BSON document containing the modifications to apply. |
| <options> | | A BOSN document specifying options. |
]]]
**Options**
[[[@cw
{ upsert: <boolean>, arrayFilters: [<document>], collation: <document>, hint: <document> }
<<@f:text>>
|@t0@r0@c0 upsert | | Specifies if a document should be created if no document matches (default: false). |
| arrayFilters | | An array of filter documents that determine which array elements to modify for an update operation on an array field. |
| collation | | A document specifying the [[https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#collation|collation]]. |
| hint | | A document specifying a [[https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/#ex-deletemany-hint|hint]]. |
<<@/f>>
]]]
**Example**
[[[@cw
db.employees.update({ salary: { $gt: 700000 }}, { $set: { bonus: 50000 }});
]]]
Sets the bonus in all documents from the employee collection with a salary greater than 700000 to 50000.
----
[[Main|home]] | [[MongoDB Driver Function Reference]] | [[MongoDB Driver]]
|