发布于 2015-09-14 14:49:24 | 252 次阅读 | 评论: 0 | 来源: 网络整理
The update() method modifies an existing document or documents in a collection. By default the update() method updates a single document. To update all documents in the collection that match the update query criteria, specify the multi option. To insert a document if no document matches the update query criteria, specify the upsert option.
在 2.2 版更改: The mongo shell provides an updated interface that accepts the options parameter in a document format to specify multi and upsert options.
Prior to version 2.2, in the mongo shell, upsert and multi were positional boolean options:
db.collection.update(query, update, <upsert>, <multi>)
The update() method takes the following parameters:
参数: |
|
---|
Although the update operation may apply mostly to updating the values of the fields, the update() method can also modify the name of the field in a document using the $rename operator.
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 an update parameter using field: value pairs and expressions using update operators as in the following:
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )
This operation updates a document in the products collection that matches the query criteria and sets the value of the field x to 6, and increment the value of the field y by 5. All other fields of the document remain the same.
To replace all the fields in a document with the document as specified in the update parameter, call the update() method with an update parameter that consists of only key: value expressions, as in the following:
db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6, y: 15 } )
This operation selects a document from the products collection that matches the query criteria sets the value of the field x to 6 and the value of the field y to 15. All other fields of the matched document are removed, except the _id field.
To update multiple documents, call the update() method and specify the multi option in the options argument, as in the following:
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { multi: true } )
This operation updates all documents in the products collection that match the query criteria by setting the value of the field x to 6 and the value of the field y to 15. This operation does not affect any other fields in documents in the products collection.
You can perform the same operation by calling the update() method with the multi parameter:
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, false, true )
To update a document or to insert a new document if no document matches the query criteria, call the update() and specify the upsert option in the options argument, as in the following:
db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, { upsert: true } )
This operation will: