Write concern guarantees the level of MongoDB write operations, the different write concern settings correspond to different levels of writes, the higher the level of setting, the better the performance of the write operation is done, but the worse the write performance. MongoDB defaults to the write concern level of acknowledged, which is also the highest level of security.
In replica set mode at the acknowledged level, MongoDB can set a wtimeout value on the client and return an error if the write cannot be completed within the specified time, even though it may eventually be able to complete.
Write Concern Levels
unacknowledged
In unacknowledged mode, the MongoDB server does not confirm that the write operation is actually received, similar to ignoring the operation. However, the drive will still attempt to receive and process errors from the network, depending on the network configuration of the system, which is a schematic of the unacknowledged level.
Acknowledgedacknowledged mode, Mongod will confirm that it received this write operation and apply this write to the in-memory data, acknowledged mode runs the client to catch all network errors, primary key conflicts and other errors. However, acknowledged does not guarantee that the write operation is always applied to disk data. Here is the schematic diagram
JournaledIn journaled mode, the MongoDB server confirms that the write is committed to the journal log, so this level ensures that MongoDB can recover all data after an unplanned outage. Of course this level must ensure that you have journaling on the server side , and each time you have to wait until the next journal Log commit, you can try to increase the journal log commit frequency.
Replica acknowledgedIf your mongodb uses a replica set, the write operation may require additional consideration, and the default setting requires only the acknowledged of the master node . In Replica acknowledged mode, it guarantees that all writes are applied to all nodes in the replica set, but the journal log here only needs to be guaranteed journal Log commit of the master node.
Available write concern driver is automatically called after each write operationGetLastError () command to determine if a write error has occurred and whether to run after the set Writeconcern, such as Db.runcommand ({getlasterror:1, w:2,j:true, wtimeout:5000}) is to confirm that the last operation W is 2,j for True,wtimeout 5000, there is no error. The Db.things.insert ({dd:123},{writeconcern:{w:2}}) is the Writeconcern that sets the currently inserted SQL;Db.setwriteconcern ({w:1,wtimeout:3000,j:1}) is the Writeconcern value that sets the current DB;
Db.getwriteconcern () is the write concern setting for the current DB;
WOption1 Default configuration to ensure that the primary node in a single instance Mongod or replica set is acknowled during a write operation0 all have no acknowled, but then if you set journal Commit acknowled words, then write operation is still acknowled mode n First N>1,n value indicates the need to acknowled Number of nodes Majority ensure that most nodes in the replica set acknowled
< Span style= "Color:rgb (73,71,71); Font-family:akzidenz, ' Helvetica Neue ', Helvetica,arial,sans-serif; font-size:14px; line-height:24px "><tag set> ensure that all nodes of this target replica set acknowled < Span style= "Color:rgb (73,71,71); Font-family:akzidenz, ' Helvetica Neue ', Helvetica,arial,sans-serif; font-size:14px; line-height:24px "> JThe OPTIONJ operation ensures that the data for the write operation is applied to the journal log on the disk, the value True indicates on, and the flase indicates off. Setting the replica set to true only guarantees that the write operation on the primary node is applied to the disk's journal log.WtimeoutThis value is only valid if the value of W is set to greater than 1, which is only valid for the replica set environment. When a timeout occurs, an error is returned, even if the final data write succeeds, MongoDB does not roll back the data that has been successfully modified. Setting this value to 0 is not a limit.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MONGODB official Website Document Read Note: write concern