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
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
.
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 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
.
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 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.
You can use one or more of the following parameters in a write concern document to specify the level of reporting.
Parameter | Value | Description |
---|---|---|
w | 0 | No acknowledgement but report on socket errors and network errors |
1 | Acknowledges the write operation on a standalone MongoDB server or the primary server of a replica set | |
>1 | A 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. | |
j | true/false | Confirms the data has been written on the journal. |
wtimeout | n | where 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}
Nothing yet..be the first to share wisdom.