Write Concern写入关注
On this page本页内容
Write concern describes the level of acknowledgment requested from MongoDB for write operations to a standalone Write concern描述了MongoDB对独立mongod
or to Replica sets or to sharded clusters. mongod
、复制集或分片集群的写操作请求的确认级别。In sharded clusters, 在分片集群中,mongos
instances will pass the write concern on to the shards.mongos
实例将把写入关注传递给分片。
For multi-document transactions, you set the write concern at the transaction level, not at the individual operation level. 对于多文档事务,您可以在事务级别设置写入关注,而不是在单个操作级别。Do not explicitly set the write concern for individual write operations in a transaction.不要为事务中的单个写入操作显式设置写入关注。
If you specify a 如果为多文档事务指定了"majority"
write concern for a multi-document transaction and the transaction fails to replicate to the calculated majority of replica set members, then the transaction may not immediately roll back on replica set members. "majority"
写入关注,而该事务未能复制到计算出的多数副本集成员,则该事务可能不会立即回滚到副本集成员。The replica set will be eventually consistent. 复制副本集最终将保持一致。A transaction is always applied or rolled back on all replica set members.事务总是在所有复制集成员上应用或回滚。
Starting in MongoDB 4.4, replica sets and sharded clusters support setting a global default write concern. 从MongoDB 4.4开始,副本集和分片集群支持设置全局默认写入关注。Operations which do not specify an explicit write concern inherit the global default write concern settings. 未指定显式写入关注的操作将继承全局默认写入关注设置。See 有关更多信息,请参阅setDefaultRWConcern
for more information.setDefaultRWConcern
。
Write Concern Specification写入关注规范
Write concern can include the following fields:写入关注可能包括以下字段:
{ w: <value>, j: <boolean>, wtimeout: <number> }
the w option to request acknowledgment that the write operation has propagated to a specified number ofmongod
instances or tomongod
instances with specified tags.w
选项用于请求确认写入操作已传播到指定数量的mongod
实例或具有指定标记的mongod
实例。the j option to request acknowledgment that the write operation has been written to the on-disk journal, andj
选项用于请求确认写入操作已写入磁盘上的日志,以及the wtimeout option to specify a time limit to prevent write operations from blocking indefinitely.wtimeout
选项用于指定一个时间限制,以防止写操作无限期阻塞。
w
Option选项
The w
option requests acknowledgment that the write operation has propagated to a specified number of mongod
instances or to mongod
instances with specified tags.w
选项请求确认写入操作已传播到指定数量的mongod
实例或具有指定标记的mongod
实例。
Using the 使用w选项,可以使用以下w
option, the following w: <value>
write concerns are available:w: <value>
写入关注:
"majority"
| members[n].votes greater than 0 ). members[n].votes 大于0 的primary和secondary)。{ w: "majority" } Note members[n].votes greater than 0 can acknowledge "majority" write operations.members[n].votes 大于0 的成员可以确认"majority" 写入操作。secondaryDelaySecs . secondaryDelaySecs 之前返回写确认。w: "majority" acknowledgment to the client, the client can read the result of that write with a "majority" readConcern.w: "majority" 确认后,客户端可以使用"majority" 读取关注读取该写入的结果。"majority" write concern for a multi-document transaction and the transaction fails to replicate to the calculated majority of replica set members, then the transaction may not immediately roll back on replica set members. "majority" 写入关注,而该事务未能复制到计算的多数副本集成员,则该事务可能不会立即回滚到副本集成员。mongod instances acknowledge the write. mongod 实例何时确认写入。 |
<number>
| mongod instances. mongod 实例。
w w: 2 would require acknowledgment from the primary and one of the secondaries. w:2 将需要来自主设备和其中一个辅助设备的确认。w: 3 would require acknowledgment from the primary and both secondaries. w:3 将需要来自主和两个辅助的确认。
Note w: <number> write operations.w: <number> 写入操作。secondaryDelaySecs . secondaryDelaySecs 之前返回写确认。mongod instances acknowledge the write. mongod 实例何时确认写入。 |
<custom write concern name>
| tagged members that satisfy the custom write concern defined in settings.getLastErrorModes . settings.getLastErrorModes 中定义的自定义写入关注的tagged 成员。mongod instances acknowledge the write. mongod 实例何时确认写入。 |
j
Option选项
The j
option requests acknowledgment from MongoDB that the write operation has been written to the on-disk journal.j
选项请求MongoDB确认写入操作已写入磁盘日志。
j
| j: true , requests acknowledgment that the mongod instances, as specified in the w: <value>, have written to the on-disk journal. j:true ,则请求确认在w: <value> 中指定的mongod 实例已写入磁盘上的日志。j: true j: true , MongoDB returns only after the requested number of members, including the primary, have written to the journal. j: true ,MongoDB只有在请求的成员数(包括primary)写入日志后才会返回。j: true write concern in a replica set only requires the primary to write to the journal, regardless of the w: <value> write concern. j: true 写入关注只需要primary 写入日志,而不考虑w: <value> 写入关注。 |
Specifying a write concern that includes将包含j: true
to amongod
instance that is running without journaling produces an error.j:true
的写入关注指定给运行时没有日志记录的mongod
实例会产生错误。If journaling is enabled,如果启用了日志,w: "majority"
may implyj: true
.w: "majority"
可能意味着j:true
。ThewriteConcernMajorityJournalDefault
replica set configuration setting determines the behavior. See Acknowledgment Behavior for details.writeConcernMajorityJournalDefault
副本集配置设置决定了行为。有关详细信息,请参阅确认行为。A write concern that includes or implies包含或暗示j: true
causes an immediate journal synchronization.j:true
的写入关注会导致立即的日志同步。See Journaling Process.请参阅日志记录过程。
wtimeout
This option specifies a time limit, in milliseconds, for the write concern. 此选项指定写入关注的时间限制(以毫秒为单位)。wtimeout
is only applicable for 仅适用于大于w
values greater than 1
.1
的w
值。
wtimeout
causes 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 当这些写操作返回时,MongoDB不会撤消在写入关注超过wtimeout
time limit.wtimeout
时间限制之前执行的成功数据修改。
If you do not specify the 如果未指定wtimeout
option and the level of write concern is unachievable, the write operation will block indefinitely. wtimeout
选项,并且无法实现写入关注级别,则写入操作将无限期地阻止。Specifying a 指定wtimeout
value of 0
is equivalent to a write concern without the wtimeout
option.wtimeout
值0
相当于不使用wtimeout
选项的写入关注。
Implicit Default Write Concern隐式默认写入关注
Starting in MongoDB 5.0, the implicit default write concern is 从MongoDB 5.0开始,隐含的默认写入关注是w: majority
. w: majority
。However, special considerations are made for deployments containing arbiters:但是,对于包含仲裁器的部署需要特别注意:
The voting majority of a replica set is 1 plus half the number of voting members, rounded down.复制集的投票多数是1加上投票成员数量的一半,四舍五入。If the number of data-bearing voting members is not greater than the voting majority, the default write concern is如果带有数据的投票成员的数量不超过投票多数,则默认的写入关注为{ w: 1 }
.{ w: 1 }
。In all other scenarios, the default write concern is在所有其他场景中,默认的写入关注是{ w: "majority" }
.{ w: "majority" }
。
Specifically, MongoDB uses the following formula to determine the default write concern:具体来说,MongoDB使用以下公式来确定默认的写入关注:
if [ (#arbiters > 0) AND (#non-arbiters <= majority(#voting-nodes)) ]
defaultWriteConcern = { w: 1 }
else
defaultWriteConcern = { w: "majority" }
For example, consider the following deployments and their respective default write concerns:例如,考虑以下部署及其各自的默认写入关注:
2 | 1 | 3 | 2 | { w: 1 } |
4 | 1 | 5 | 3 | { w: "majority" } |
In the first example:在第一个示例中:There are 2 non-arbiters and 1 arbiter for a total of 3 voting nodes.对于总共3个投票节点,存在2个非仲裁器和1个仲裁器。The majority of voting nodes (1 plus half of 3, rounded down) is 2.大多数投票节点(1加3的一半,四舍五入)为2。The number of non-arbiters (2) is equal to the majority of voting nodes (2), resulting in an implicit write concern of非仲裁器的数量(2)等于大多数投票节点(2),导致{ w: 1 }
.{ w: 1 }
的隐式写入关注。
In the second example:在第二个示例中:There are 4 non-arbiters and 1 arbiter for a total of 5 voting nodes.对于总共5个投票节点,有4个非仲裁器和1个仲裁器。The majority of voting nodes (1 plus half of 5, rounded down) is 3.大多数投票节点(1加5的一半,四舍五入)为3。The number of non-arbiters (4) is greater than the majority of voting nodes (3), resulting in an implicit write concern of非仲裁器的数量(4)大于大多数投票节点(3),导致{ w: "majority" }
.{ w: "majority" }
的隐式写入关注。
Acknowledgment Behavior确认行为
The w option and the j option determine when mongod
instances acknowledge write operations.w
选项和j
选项决定mongod
实例何时确认写入操作。
Standalone单机版
A standalone 独立的mongod
acknowledges a write operation either after applying the write in memory or after writing to the on-disk journal. mongod
在内存中应用写操作或在写入磁盘日志后确认写操作。The following table lists the acknowledgment behavior for a standalone and the relevant write concerns:下表列出了独立程序的确认行为和相关的写入关注:
j | j:true | j:false | |
---|---|---|---|
w: 1 | |||
w: "majority" |
With 在writeConcernMajorityJournalDefault
set to false
, MongoDB does not wait for w: "majority"
writes to be written to the on-disk journal before acknowledging the writes. writeConcernMajorityJournalDefault
设置为false
的情况下,MongoDB不会等待w: "majority"
写入到磁盘日志中,然后再确认写入。As such, 因此,在给定副本集中的大多数节点发生瞬时丢失(例如崩溃和重新启动)的情况下,"majority"
write operations could possibly roll back in the event of a transient loss (e.g. crash and restart) of a majority of nodes in a given replica set."majority"
写入操作可能会回滚。
Replica Sets复制集
The value specified to w determines the number of replica set members that must acknowledge the write before returning success. 为w
指定的值决定了在返回成功之前必须确认写入的副本集成员数。For each eligible replica set member, the j option determines whether the member acknowledges writes after applying the write operation in memory or after writing to the on-disk journal.对于每个符合条件的副本集成员,j
选项确定该成员是在内存中应用写入操作后还是在写入磁盘日志后确认写入。
w: "majority"
-
Any data-bearing voting member of the replica set can contribute to write acknowledgment of副本集的任何带有投票权的数据成员都可以对"majority"
write operations."majority"
写入操作的写入确认做出贡献。The following table lists when the member can acknowledge the write based on the j value:下表列出了成员何时可以根据j
值确认写入:j
is unspecified未指定Acknowledgment depends on the value of确认取决于writeConcernMajorityJournalDefault
:writeConcernMajorityJournalDefault
的值:If如果为true
, acknowledgment requires writing operation to on-disk journal (j: true
).true
,则确认需要将操作写入磁盘上的日志(j: true
)。
writeConcernMajorityJournalDefault
defaults to默认为true
If如果为false
, acknowledgment requires writing operation in memory (j: false
).false
,则确认需要在内存中进行写入操作(j: false
)。
j: true
Acknowledgment requires writing operation to on-disk journal.确认需要将操作写入磁盘日志。j: false
Acknowledgment requires writing operation in memory.确认需要在内存中进行写入操作。NoteFor behavior details, see有关行为的详细信息,请参阅w: "majority"
Behavior.w: "majority"
行为。 w: <number>
-
Any data-bearing member of the replica set can contribute to write acknowledgment of w: <number> write operations.副本集的任何承载数据的成员都可以对w: <number>
写入操作的写入确认做出贡献。The following table lists when the member can acknowledge the write based on the j value:下表列出了成员何时可以根据j
值确认写入:j
is unspecified未指定Acknowledgment requires writing operation in memory (确认需要在内存中进行写入操作(j: false
).j:false
)。j: true
Acknowledgment requires writing operation to on-disk journal.确认需要将操作写入磁盘日志。j: false
Acknowledgment requires writing operation in memory.确认需要在内存中进行写入操作。
Hidden, delayed, and priority 0 members can acknowledge 隐藏、延迟或优先级0成员可以确认w: <number>
write operations.w: <number>
写入操作。
Delayed secondaries can return write acknowledgment no earlier than the configured 延迟的secondary可以在配置的secondaryDelaySecs
.secondaryDelaySecs
之前返回写确认。
Additional Information附加信息
Causally Consistent Sessions and Write Concerns因果一致的会话和写入关注
With causally consistent client sessions, the client sessions only guarantee causal consistency if:对于因果一致的客户端会话,只有在以下情况下,客户端会话才能保证因果一致性:
the associated read operations use关联的读取操作使用"majority"
read concern, and"majority"
读取关注,以及the associated write operations use关联的写入操作使用"majority"
write concern."majority"
写入关注。
For details, see Causal Consistency.有关详细信息,请参阅因果一致性。
w: "majority"
Behavior行为
With在writeConcernMajorityJournalDefault
set tofalse
, MongoDB does not wait forw: "majority"
writes to be written to the on-disk journal before acknowledging the writes.writeConcernMajorityJournalDefault
设置为false
的情况下,MongoDB不会等待w: "majority"
写入到磁盘日志中,然后再确认写入。As such,因此,在给定副本集中的大多数节点发生瞬时丢失(例如崩溃和重新启动)的情况下,"majority"
write operations could possibly roll back in the event of a transient loss (e.g. crash and restart) of a majority of nodes in a given replica set."majority"
写入操作可能会回滚。Hidden, delayed, and priority 0 members with隐藏、延迟或优先级0,且members[n].votes
greater than0
can acknowledge"majority"
write operations.members[n].votes
大于0
的成员可以确认"majority"
写入操作。Delayed secondaries can return write acknowledgment no earlier than the configured延迟的secondary可以在配置的secondaryDelaySecs
.secondaryDelaySecs
之前返回写确认。
Starting in MongoDB 5.0, replica set members in the从MongoDB 5.0开始,处于STARTUP2
state do not participate in write majorities.STARTUP2
状态的副本集成员不参与写入多数。
Write Concern not Supported on local
Databaselocal
数据库不支持写入关注
local
DatabaseThe local database does not support write concerns. MongoDB silently ignores any configured write concern for an operation on a collection in the local database.local
数据库不支持写入关注。MongoDB静默地忽略本地数据库中对集合的操作所配置的任何写入关注。
Calculating Majority for Write Concern计算写入关注的多数
Starting in version 4.2.1, the 从4.2.1版本开始,rs.status()
returns the writeMajorityCount
field which contains the calculated majority number.rs.status()
返回writeMajorityCount
字段,该字段包含计算的多数数值。
The majority for write concern 写入关注"majority"
is calculated as the smaller of the following values:"majority"
的多数计算为以下值中的较小值:
the majority of all voting members (including arbiters) vs.所有有投票权的成员(包括仲裁人)的多数vs.the number of all data-bearing voting members.所有携带数据的投票成员的人数。
In cases where the calculated majority number is equal to the number of all data-bearing voting members (such as with a 3-member Primary-Secondary-Arbiter deployment), write concern 在计算出的多数人数等于所有携带数据的投票成员的人数的情况下(例如,部署了3名成员的Primary-Seconary-Arbiter),如果携带数据的表决成员出现故障或无法访问,则写入关注"majority"
may time out or never be acknowledged if a data bearing voting member is down or unreachable. "majority"
可能会超时或永远不会得到确认。If possible, use a data-bearing voting member instead of an arbiter.如果可能的话,请使用带有数据的投票成员,而不是仲裁员。
For example, consider:例如,考虑:
A replica set with 3 voting members, Primary-Secondary-Secondary (P-S-S):一个包含3个投票成员的复制集,Primary-Secondary-Secondary(P-S-S):The majority of all voting members is 2.所有有表决权的成员的多数为2人。The number of all data-bearing voting members is 3.所有有数据表决权的成员人数为3人。
The calculated majority is 2, the minimum of 2 and 3.计算出的多数是2,2和3的最小值。The write must propagate to the primary and one of the secondaries to acknowledge the write concern写入必须传播到主要和其中一个次要,以向客户端确认写入关注"majority"
to the client."majority"
。A replica set with 3 voting members, Primary-Secondary-Arbiter (P-S-A)一个由3名投票成员组成的复制集,Primary-Secondary-Arbiter(P-S-A)The majority of all voting members is 2.所有有表决权的成员的多数为2人。The number of all data-bearing voting members is 2.所有有数据表决权的成员人数为2人。
The calculated majority is 2, the minimum of 2 and 2. Since the write can only be applied to data-bearing members, the write must propagate to the primary and the secondary to acknowledge the write concern计算出的多数是2,2和2的最小值。由于写入只能应用于承载数据的成员,因此写入必须传播到主成员和辅助成员,以向客户端确认写入关注"majority"
to the client."majority"
。TipAvoid using a避免在(P-S-a)或其他拓扑结构中使用"majority"
write concern with a (P-S-A) or other topologies that require all data-bearing voting members to be available to acknowledge the writes."majority"
写入关注,这些拓扑结构要求所有带数据的投票成员都可以确认写入。Customers who want the durability guarantees of using a想要使用"majority"
write concern should instead deploy a topology that does not require all data bearing voting members to be available (e.g. P-S-S)."majority"
写入关注的持久性保证的客户应该部署一种拓扑结构,该拓扑结构不需要所有带数据的投票成员都可用(例如P-S-S)。
Avoid deploying more than one arbiter in a replica set. See Concerns with Multiple Arbiters.避免在副本集中部署多个仲裁器。请参阅与多个仲裁器有关的问题。
To add an arbiter to an existing replica set:要将仲裁器添加到现有副本集,请执行以下操作:
Typically, if there are two or fewer data-bearing members in the replica set, you might need to first set the cluster wide write concern for the replica set.通常,如果副本集中有两个或更少的数据承载成员,则可能需要首先为副本集设置集群范围的写入关注。See cluster wide write concern for more information on why you might need to set the cluster wide write concern.有关为什么可能需要设置集群范围的写入关注的更多信息,请参阅集群范围的写入关注。
You do not need to change the cluster wide write concern before starting a new replica set with an arbiter.在使用仲裁器启动新的副本集之前,不需要更改集群范围内的写入关注。
See also: 另请参阅:
Write Concern Provenance撰写关注来源
Starting in version 4.4, MongoDB tracks write concern 从4.4版本开始,MongoDB跟踪写入关注的provenance
, which indicates the source of a particular write concern. provenance
,这表明了特定写入关注的源。You may see 您可能会在provenance
shown in the getLastError
metrics, write concern error objects, and MongoDB logs.getLastError
度量、写入关注错误对象和MongoDB日志中看到出处。
The following table shows the possible write concern 下表显示了可能的写入关注provenance
values and their significance:provenance
值及其重要性:
clientSupplied | |
customDefault | setDefaultRWConcern .setDefaultRWConcern 。 |
getLastErrorDefaults | settings.getLastErrorDefaults field.settings.getLastErrorDefaults 字段。 |
implicitDefault |
Write Concern Contrasted with Commit Quorum写入关注与提交法定人数对比
There are important differences between commit quorums and write concerns:提交法定人数和写入关注之间存在重要差异:
Index builds use commit quorums.索引构建使用提交法定人数。Write operations use write concerns.写操作使用写入关注。
Each data-bearing node in a cluster is a voting member.集群中的每个数据承载节点都是一个投票成员。
The commit quorum specifies how many data-bearing voting members, or which voting members, including the primary, must be prepared to commit a simultaneous index build. before the primary will execute the commit.提交法定人数指定有多少数据承载投票成员,或者必须准备哪些投票成员(包括主要成员)来提交同时索引构建。在主服务器执行提交之前。
The write concern is the level of acknowledgement that the write has propagated to the specified number of instances.写入关注是确认写入已传播到指定数量的实例的级别。
The commit quorum specifies how many nodes must be ready to finish the index build before the primary commits the index build. 提交法定人数指定在主提交索引生成之前,必须准备好多少节点才能完成索引生成。In contrast, when the primary has committed the index build, the write concern specifies how many nodes must finish the index build before the command returns.相反,当主节点提交了索引构建时,写入关注指定在命令返回之前必须有多少节点完成索引构建。