On this page本页内容
Write concern describes the level of acknowledgment requested from MongoDB for write operations to a standalone 写入关注点描述了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
实例将把写关注点传递给分片。
Note
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.不要为事务中的单个写入操作显式设置写入关注点。
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 can include the following fields:写关注点可以包括以下字段:
mongod
instances or to mongod
instances with specified tags.w
选项请求确认写入操作已传播到指定数量的mongod
实例或具有指定标记的mongod
实例。j
选项请求确认写入操作已写入磁盘日志,以及wtimeout
选项指定一个时间限制,以防止写操作无限期阻塞。w
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
option, the following w: <value>
write concerns are available:w
选项,可以使用以下&w:lt;value>
写关注点:
Value | |
---|---|
|
Note
|
|
Note
|
|
|
j
The j
option requests acknowledgment from MongoDB that the write operation has been written to the on-disk journal.j
选项请求MongoDB确认写入操作已写入磁盘日志。
|
|
Note
j: true
to a mongod
instance that is running without journaling produces an error.mongod
实例指定包含j:true
的写关注点会产生错误。w: "majority"
may imply j: true
. w: "majority"
可能意味着j:true
。writeConcernMajorityJournalDefault
replica set configuration setting determines the behavior. writeConcernMajorityJournalDefault
副本集配置设置决定了行为。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
选项的写关注点。
The w option and the j option determine when mongod
instances acknowledge write operations.w
选项和j
选项确定mongod
实例何时确认写操作。
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" |
Note
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
写入操作可能会回滚。
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 |
|
---|---|
j: true | |
j: false |
Note
For 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 | j: false ).j:false )。 |
---|---|
j: true | |
j: false |
Note
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 延迟的二级缓存可以在配置的slaveDelay
.slaveDelay
之前返回写确认。
With causally consistent client sessions, the client sessions only guarantee causal consistency if:对于因果一致的客户端会话,客户端会话仅在以下情况下保证因果一致性:
"majority"
read concern, and"majority"
读取关注点,并且"majority"
write concern."majority"
写入关注点。For details, see Causal Consistency.有关详细信息,请参阅因果一致性。
w: "majority"
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"
写操作写入磁盘日志。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
写入操作可能会回滚。members[n].votes
greater than 0
can acknowledge "majority"
write operations.members[n].votes
大于0
的成员可以确认“多数”写入操作。slaveDelay
.slaveDelay
之前返回写确认。STARTUP2
state do not participate in write majorities.STARTUP2
状态下的副本集成员不参与写入多数。local
Databaselocal
数据库不支持写关注点¶The local database does not support write concerns. 本地数据库不支持写关注点。MongoDB silently ignores any configured write concern for an operation on a collection in the local database.MongoDB会自动忽略本地数据库中集合操作的任何已配置写关注点。
Tip
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"
的多数计算为以下值中的较小值:
Warning警告
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 如果计算出的多数数等于所有携带数据的投票成员的数量(例如使用三人主从仲裁员部署),如果携带数据的投票成员失败或无法访问,则"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:例如,考虑:
"majority"
to the client."majority"
。"majority"
to the client."majority"
。Tip
Avoid using a 避免在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)。
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日志中看到provenance
。
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 |