发布于 2015-09-14 14:49:24 | 252 次阅读 | 评论: 0 | 来源: 网络整理

db.collection.update(query, update[, options])

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:

参数:
  • query (document) – Specifies the selection criteria for the update. The query parameter employs the same query selectors as used in the db.collection.find() method.
  • update (document) –

    Specifies the modifications to apply.

    If the update parameter contains any update operators expressions such as the $set operator expression, then:

    • the update parameter must contain only update operators expressions.
    • the update() method updates only the corresponding fields in the document.

    If the update parameter consists only of field: value expressions, then:

    • the update() method replaces the document with the updates document. If the updates document is missing the _id field, MongoDB will add the _id field and assign to it a unique objectid .
    • the update() method updates cannot update multiple documents.
  • options (document) –

    2.2 新版功能.

    Optional. Specifies whether to perform an upsert and/or a multiple update. Use the options parameter instead of the individual upsert and multi parameters.

  • upsert (boolean) –

    Optional. Specifies an upsert operation

    The default value is false. When true, the update() method will update an existing document that matches the query selection criteria or if no document matches the criteria, insert a new document with the fields and values of the update parameter and if the update included only update operators, the query parameter as well .

    In version 2.2 of the mongo shell, you may also specify upsert in the options parameter.

    注解

    An upsert operation affects only one document, and cannot update multiple documents.

  • multi (boolean) –

    Optional. Specifies whether to update multiple documents that meet the query criteria.

    When not specified, the default value is false and the update() method updates a single document that meet the query criteria.

    When true, the update() method updates all documents that meet the query criteria.

    In version 2.2 of the mongo shell, you may also specify multi in the options parameter.

    注解

    The multi update operation may interleave with other write operations. For unsharded collections, you can override this behavior with the $isolated isolation operator, which isolates the update operation and blocks other write operations during the update. See the isolation operator.

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:

    • update a single document in the products collection that matches the query criteria, setting the value of the field x to 25 and the value of the field y to 50, or
    • if no matching document exists, insert a document in the products collection, with the field item set to magazine, the field x set to 25, and the field y set to 50.
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务