- Reference >
- JavaScript Methods >
- db.collection.update()
db.collection.update()¶
-
db.collection.update(query, update[, options])¶ The
update()method modifies an existing document or documents in a collection. By default theupdate()method updates a single document. To update all documents in the collection that match the update query criteria, specify themultioption. To insert a document if no document matches the update query criteria, specify theupsertoption.Changed in version 2.2: The
mongoshell provides an updated interface that accepts the options parameter in a document format to specifymultiandupsertoptions.Prior to version 2.2, in the
mongoshell,upsertandmultiwere positional boolean options:The
update()method takes the following parameters:Parameters: - query (document) – Specifies the selection criteria for the
update. The
queryparameter employs the same query selectors as used in thedb.collection.find()method. - update (document) –
Specifies the modifications to apply.
If the
updateparameter contains any update operators expressions such as the$setoperator expression, then:- the
updateparameter must contain onlyupdate operatorsexpressions. - the
update()method updates only the corresponding fields in the document.
If the
updateparameter consists only offield: valueexpressions, then: - the
- options (document) –
New in version 2.2.
Optional. Specifies whether to perform an upsert and/or a multiple update. Use the
optionsparameter instead of the individualupsertandmultiparameters. - upsert (boolean) –
Optional. Specifies an upsert operation
The default value is
false. Whentrue, theupdate()method will update an existing document that matches thequeryselection criteria or if no document matches the criteria, insert a new document with the fields and values of theupdateparameter and if theupdateincluded onlyupdate operators, thequeryparameter as well .In version 2.2 of the
mongoshell, you may also specifyupsertin theoptionsparameter.Note
With
upsertupdate()inserts a single document. - multi (boolean) –
Optional. Specifies whether to update multiple documents that meet the query criteria.
When not specified, the default value is
falseand theupdate()method updates a single document that meet thequerycriteria.When
true, theupdate()method updates all documents that meet thequerycriteria.In version 2.2 of the
mongoshell, you may also specifymultiin theoptionsparameter.Note
The
multiupdate operation may interleave with other write operations. For unsharded collections, you can override this behavior with the$isolatedoperator, which isolates the update operation and disallows yielding during the operation. This isolates the update so that no client can see the updated documents until they are all processed, or an error stops the update operation. See$isolated.
Although the update operation may apply mostly to updating the values of the fields, the
update()method can also modify the name of thefieldin a document using the$renameoperator.Consider the following examples of the
update()method. These examples all use the 2.2 interface to specify options in the document form.To update specific fields in a document, call the
update()method with anupdateparameter usingfield: valuepairs and expressions using update operators as in the following:This operation updates a document in the
productscollection that matches the query criteria and sets the value of the fieldxto6, and increment the value of the fieldyby5. All other fields of the document remain the same.To replace all the fields in a document with the document as specified in the
updateparameter, call theupdate()method with anupdateparameter that consists of onlykey: valueexpressions, as in the following:This operation selects a document from the
productscollection that matches the query criteria sets the value of the fieldxto6and the value of the fieldyto15. All other fields of the matched document are removed, except the _id field.To update multiple documents, call the
update()method and specify themultioption in theoptionsargument, as in the following:This operation updates all documents in the
productscollection that match the query criteria by setting the value of the fieldxto6and the value of the fieldyto15. This operation does not affect any other fields in documents in theproductscollection.You can perform the same operation by calling the
update()method with themultiparameter:To update a document or to insert a new document if no document matches the query criteria, call the
update()and specify theupsertoption in theoptionsargument, as in the following:This operation will:
- update a single document in the
productscollection that matches the query criteria, setting the value of the fieldxto25and the value of the fieldyto50, or - if no matching document exists, insert a document in the
productscollection, with the fielditemset tomagazine, the fieldxset to25, and the fieldyset to50.
- update a single document in the
- query (document) – Specifies the selection criteria for the
update. The