mongodb "Write Concern"
2017-03-29 09:42
399 查看
Write Concern
On this pageWrite
Concern Specification
Acknowledgement
Behavior
Write concern describes the level of acknowledgement requested from MongoDB for write operations to a standalone
mongodor
to replica sets or to sharded
clusters. In sharded clusters,
mongosinstances
will pass the write concern on to the shards.
Changed in version 2.6: A new protocol for write
operations integrates write concerns with the write operations and eliminates the need to call the
getLastErrorcommand.
Previous versions required a
getLastErrorcommand
immediately after a write operation to specify the write concern.
Write Concern Specification
Write concern can include the following fields:{ w: <value>, j: <boolean>, wtimeout: <number> }
the w option to request acknowledgement that the write operation
has propagated to a specified number of
mongodinstances
or to
mongodinstances
with specified tags.
the j option to request acknowledgement that the write operation
has been written to the journal, and
the wtimeout option to specify a time limit to prevent
write operations from blocking indefinitely.
w
Option
The woption requests acknowledgement that the write operation has propagated to a specified number of
mongodinstances
or to
mongodinstances
with specified tags.
Using the
woption, the following
w: <value>write
concerns are available:
Value | Description |
---|---|
<number> | Requests acknowledgement that the write operation has propagated to the specified number ofmongodinstances. For example: w: 1 Requests acknowledgement that the write operation has propagated to the standalone mongodor the primary in a replica set. w: 1is the default write concern for MongoDB. w: 0 Requests no acknowledgement of the write operation. However, w: 0may return information about socket exceptions and networking errors to the application. If you specify w: 0but include j: true, the j: true prevails to request acknowledgement from the standalone mongodor the primary of a replica set. Numbers greater than 1 are valid only for replica sets to request acknowledgement from specified number of members, including the primary. See Acknowledgement Behavior for when mongodinstances acknowledge the write. |
"majority" | Requests acknowledgement that write operations have propagated to the majority of voting nodes[2], including the primary. After the write operation returns with a w: "majority"acknowledgement to the client, the client can read the result of that write with a "majority"readConcern. See Acknowledgement Behavior for when mongodinstances acknowledge the write. |
<tag set> | Requests acknowledgement that the write operations have propagated to a replica set member with the specified tag. See Acknowledgement Behavior for when mongodinstances acknowledge the write. |
Replica Set Protocol Versions
j
Option
The joption requests acknowledgement from MongoDB that the write operation has been written to thejournal.
j | If j: true, requests acknowledgement that the mongodinstances, as specified in the w: <value>, have written to the on-disk journal. j: truedoes not by itself guarantee that the write will not be rolled back due to replica set primary failover. Changed in version 3.2: With j: true, MongoDB returns only after the requested number of members, including the primary, have written to the journal. Previously j: truewrite concern in a replica set only requires the primary to write to the journal, regardless of the w: <value> write concern. |
Specifying a write concern that includes
j: trueto
a
mongodinstance
that is running without journaling produces an error.
For replica sets using
protocolVersion: 1,
if journaling is enabled,
w: "majority"may
imply
j: true. The
writeConcernMajorityJournalDefaultreplica
set configuration setting determines the behavior. See Acknowledgement Behavior for
details.
wtimeout
This option specifies a time limit, in milliseconds, for the write concern. wtimeoutis only applicable for
wvalues
greater than
1.
wtimeoutcauses write operations to return with an error after the specified limit, even if
the required write concern will eventually succeed. When these write operations return, MongoDB does not undo successful data modifications performed before the write concern exceeded the
wtimeouttime
limit.
If you do not specify the
wtimeoutoption and the level of write concern is unachievable, the
write operation will block indefinitely. Specifying a
wtimeoutvalue of
0is
equivalent to a write concern without the
wtimeoutoption.
Acknowledgement Behavior
The w option and the j optiondetermine when
mongodinstances
acknowledge write operations.
Standalone
A standalonemongodacknowledges
a write operation either after applying the write in memory or after writing to the on-disk journal. The following table lists the acknowledgement behavior for a standalone and the relevant write concerns:
jis unspecified | j:true | j:false | |
---|---|---|---|
w: 1 | In memory | On-disk journal | In memory |
w: "majority" | On-disk journal if running with journaling | On-disk journal | In memory |
With
writeConcernMajorityJournalDefaultset
to
false, MongoDB will not wait for
w:"majority"writes
to be durable before acknowledging the writes. As such,
"majority"write operations could possibly roll
back in the event of a loss of a replica set member.
Replica Sets
Changed in version 3.4.A replica set acknowledges a write operation either after the specified number of members apply the write in memory or after they write to the on-disk journal. The number of members is specified by the w:
<value> setting. The following table lists the acknowledgement behavior for these members and the relevant write concerns [1]:
jis unspecified | j:true | j:false | |
---|---|---|---|
w: "majority" | Depends on the value ofwriteConcernMajorityJournalDefault. If true, On-disk journal. If false, In memory. Changed in version 3.4. | On-disk journal | In memory |
w: <number> | In memory | On-disk journal | In memory |
With
writeConcernMajorityJournalDefaultset
to
false, MongoDB will not wait for
w:"majority"writes
to be durable before acknowledging the writes. As such,
"majority"write operations could possibly roll
back in the event of a loss of a replica set member.
[1] | For the behavior in version 3.2, refer to the 3.2 manual. |
[2] | Changed in version 3.0: Prior to MongoDB 3.0, w: "majority"refers to the majority of the replica set’s members instead of the majority of the replica set’s voting members. Changed in version 2.6: In Master/Slave deployments, MongoDB treats w: "majority"as equivalent to w: 1. In earlier versions of MongoDB, w: "majority"produces an error in master/slave deployments. |
相关文章推荐
- MongoDB Write Concern整理
- 转载:在恰当的地方使用MongoDB的WriteConcern.SAFE参数
- 故障案例--mongodb writeconcern为majority时的又一个bug
- MongoDB WriteConcern
- MongoDB Write Concern整理
- Spirng中Mongodb中write-concern的解释
- MongoDB Write Concern整理
- 在恰当的地方使用MongoDB的WriteConcern.SAFE参数
- 在恰当的地方使用MongoDB的WriteConcern.SAFE参数
- PHP 使用 Mongodb driver:Call to undefined method MongoDB\Driver\WriteConcern::isDefault()
- 解决ssh的"Write failed: Broken pipe"问题
- Error code: 0x80004005 "The Microsoft Access database engine cannot open or write to the file ''
- MongoDB服务启动时报"Windows 无法启动Mongo DB服务 错误:1067 进程意外终止"
- Response.Write(" alert( 中加参数
- testng报错"java.net.SocketException: Software caused connection abort: socket write error"
- MySQL5.7 切不要"乱射" --transaction-write-set-extraction=MURMUR32
- ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error"异常原因分析
- ASM diskgroup dismount with "Waited 15 secs for write IO to PST" (文档 ID 1581684.1)
- root用户在hdfs 上创建文件报错:mkdir: Permission denied: user=root, access=WRITE, inode="/":hdfs:supergroup:drw