Update Options

Let's revisit the update() method syntax discussed in the previous chapter.

db.CollectionName.update(
   <update_condition>,
   <update_document>,
   { upsert: <boolean>, multi: <boolean>,  writeConcern: {<document>} }
)

In the above syntax, <update_condition> and <update_document> parameters are followed by a document containing the following parameters

{
upsert : <boolean>,
multi : <boolean>, 
writeConcern: {<document>}
}

You can call this document an options_document. The parameters in an options document are optional and the options document itself is optional. The rest of this chapters explains each of these parameters in the options document

multi

The update method updates only the first matching document if you omit the options document. To update multiple documents that match the update_condition, you should pass an options_document with the multi parameter set to true.

Example

The following update operation updates all documents that have director field value equals Quentin Tarantino and set the genre field with value Action

db.movie.update( { director: "Quentin Tarantino" },
 { $set: { genre: "Action" } }, 
 { multi:true }
 ) 
>
> db.movie.find({director:"Quentin Tarantino"},{title:1,genre:1})
{ "_id" : ObjectId("53623324995b194b63078522"), "genre" : "Action"
, "title" : "Pulp Fiction" }
{ "_id" : ObjectId("53623324995b194b63078523"), "genre" : "Action"
, "title" : "Django Unchained" }
>

The multi parameter can have a boolean value - true or false, the default value if omitted is false

upsert

The update() method updates document(s) that match the update_condition. If no documents match the update_condition you could insert a new document by setting upsert parameter to true.

Example

The following example will first try to update the document with title field equals "Non-Stop". As there are no documents matching that title, it will be inserted in to the collection.

db.movie.update( { title: "Non-Stop" },
 { $push: { cast: "Liam Neeson" } }, 
 { upsert:true }
 )
>
> db.movie.find( {title:"Non-Stop"} )
{ "_id" : ObjectId("53baf7d899d8dd0a5ffdb3ad"), "cast" : [  "Liam 
Neeson" ], "title" : "Non-Stop" }
>

Write Concern

Write concern specifies the level of reporting needed when performing write operations such as insert, update and delete.

A weak write concern mean less guarantee that the operation has succeeded but gives a faster performance as the clients do not wait for the result of the write operation.

A strong write concern ensures the operation completes successfully but with slower performance as clients have to wait for confirmation of successful write operation.

MongoDB provides the following levels of write concern.

  1. Unacknowledged: In this level MongoDB does not acknowledge the result of the write operation. This is the weakest level.
  2. Acknowledged: In this level MongoDB acknowledges the receipt of write operation.
  3. Journaled: Acknowledges the write operation after committing to the journal (transaction log).
  4. Replica Acknowledged: Ensures the write operation is propagated to all members of a replica set (cluster of MongoDB servers).

You can use one or more of the following parameters in a write concern document to specify the level of reporting.

ParameterValueDescription
w0No acknowledgement but report on socket errors and network errors
1Acknowledges the write operation on a standalone MongoDB server or the primary server of a replica set
>1A value greater than 1 means the write operation is successfully propagated to that many members in a replica set including the primary.
"majority"Ensures the write operation is propagated to majority members of a replica set.
<tag set>You can specify which members of the replica set must acknowledge the write operation.
jtrue/falseConfirms the data has been written on the journal.
wtimeoutnwhere n is time limit in milliseconds write operation should wait to get the write concern. When this limit is reached, write operation is returned even if the write concern is not achieved.

The following is an example of a write concern document which can be used with write operations such as update.

{w:1, j:true, wtimeout:1000}
Last Updated: 08-July-2014


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Abhi | November 15, 2016 4:36 PM |

How to update multiple documents with different values at a bulk..

Mukesh Rao | December 3, 2016 1:28 PM |

Well explained. However, a diagram would have helped. Thanks