update() - Update JSON documents command

Updates a JSON document or documents in a collection.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. update(query,updFields upsert ,multi )

Command parameters

query
This parameter specifies a filter to select a subset of documents. The command can return an empty result set.
updFields
This parameter specifies an object with attribute names and values that must be updated. This parameter uses the $set operator to set the new value for a field.
upsert (true | false)
If the value is true, this optional parameter updates a document or inserts missing documents. If the value is false, it updates only the documents. The default value is false.
multi (true | false)
If the value is true, this optional parameter updates all matching documents. If the value is false, only the first matching row is updated. The default value is false.

Example

Update the specified fields in a document:
db.books.update({isbn: "123-456-234"}, {$set: {pages: 299}})
Sample output is as follows:
Updated 1 rows.