Write Concern写入关注

On this page本页内容

Write concern describes the level of acknowledgment requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters. 写入关注描述了MongoDB请求的对独立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.不要为事务中的单个写入操作显式设置写入关注点。

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 of mongod instances or to mongod 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 option, the following w: <value> write concerns are available:使用w选项,可以使用以下w:<value>写关注点:

ValueDescription描述
"majority"

Requests acknowledgment that write operations have propagated to the calculated majority of the data-bearing voting members (i.e. primary and secondaries with members[n].votes greater than 0). 请求确认写入操作已传播到计算出的大多数数据承载投票成员(即members[n].votes大于0的主要和次要成员)。{ w: "majority" } is the default write concern for most MongoDB deployments. 大多数MongoDB部署的默认写入问题。See Implicit Default Write Concern.请参阅隐式默认写入关注点

For example, consider a replica set with 3 voting members, Primary-Secondary-Secondary (P-S-S). 例如,考虑一个具有3个投票成员的副本集,即主-次(P-S-S)。For this replica set, calculated majority is two, and the write must propagate to the primary and one secondary to acknowledge the write concern to the client.对于该副本集,计算多数是两个,写入必须传播到主副本和一个次副本,以向客户端确认写入问题。

Note注意

Hidden, delayed, and priority 0 members with members[n].votes greater than 0 can acknowledge "majority" write operations.members[n].votes大于0隐藏延迟优先级为0的成员可以确认"majority"写入操作。

Delayed secondaries can return write acknowledgment no earlier than the configured secondaryDelaySecs.延迟的二级设备可以在配置的secondaryDelaySecs之前返回写确认。

After the write operation returns with a w: "majority" acknowledgment to the client, the client can read the result of that write with a "majority" readConcern.在写操作返回给客户端一个w: "majority"确认后,客户端可以使用"majority"读取关注读取该写操作的结果。

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.事务始终应用于或回滚所有副本集成员。

See Acknowledgment Behavior for when mongod instances acknowledge the write.有关mongod实例何时确认写入,请参阅确认行为。

<number>

Requests acknowledgment that the write operation has propagated to the specified number of mongod instances. 请求确认写入操作已传播到指定数量的mongod实例。For example:例如:

w: 1
Requests acknowledgment that the write operation has propagated to the standalone mongod or the primary in a replica set. 请求确认写入操作已传播到副本集中的独立mongod或主mongodData can be rolled back if the primary steps down before the write operations have replicated to any of the secondaries.如果在将写操作复制到任何辅助设备之前,主设备退出,则可以回滚数据
w: 0

Requests no acknowledgment of the write operation. 不请求对写入操作的确认。However, w: 0 may return information about socket exceptions and networking errors to the application. 但是,w: 0可能会向应用程序返回有关套接字异常和网络错误的信息。Data can be rolled back if the primary steps down before the write operations have replicated to any of the secondaries.如果在写入操作复制到任何辅助设备之前,主设备退出,则可以回滚数据。

If you specify w: 0 but include j: true, the j: true prevails to request acknowledgment from the standalone mongod or the primary of a replica set.如果指定w:0但包含j:true,则以j:true为准从独立mongod或副本集的主服务器请求确认。

w greater than 1 requires acknowledgment from the primary and as many data-bearing secondaries as needed to meet the specified write concern. 大于1需要来自主设备和尽可能多的数据承载辅助设备的确认,以满足指定的写关注点。For example, consider a 3-member replica set with a primary and 2 secondaries. 例如,考虑一个具有一个主副本和两个辅助副本的三成员副本集。Specifying w: 2 would require acknowledgment from the primary and one of the secondaries. 指定w:2将需要主设备和其中一个辅助设备的确认。Specifying w: 3 would require acknowledgment from the primary and both secondaries.指定w:3将需要主设备和两个辅助设备的确认。

Note注意

Hidden, delayed, and priority 0 members can acknowledge w: <number> write operations.隐藏延迟优先级为0的成员可以确认w: <number>写入操作。

Delayed secondaries can return write acknowledgment no earlier than the configured secondaryDelaySecs.延迟的二级设备可以在配置的secondaryDelaySecs之前返回写确认。

See Acknowledgment Behavior for when mongod instances acknowledge the write.有关mongod实例何时确认写入,请参阅确认行为

<custom write concern name>

Requests acknowledgment that the write operations have propagated to tagged members that satisfy the custom write concern defined in settings.getLastErrorModes. 请求确认写入操作已传播到满足settings.getLastErrorModes中定义的自定义写入关注点的tagged成员。For an example, see Custom Multi-Datacenter Write Concerns.有关示例,请参阅自定义多数据中心写入关注点

Data can be rolled back if the custom write concern only requires acknowledgment from the primary and the primary steps down before the write operations have replicated to any of the secondaries.如果自定义写入关注点只需要来自主服务器的确认,并且在写入操作复制到任何辅助服务器之前,主服务器逐步退出,则可以回滚数据

See Acknowledgment Behavior for when 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

If 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 does not by itself guarantee that the write will not be rolled back due to replica set primary failover.其本身并不保证写入不会由于副本集主故障切换而回滚。

Changed in version 3.2.在版本3.2中更改

With j: true, MongoDB returns only after the requested number of members, including the primary, have written to the journal. 对于j: true,MongoDB仅在请求的成员数(包括主成员)写入日志后返回。Previously 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>写入关注点无关。

Note注意

wtimeout

This option specifies a time limit, in milliseconds, for the write concern. 此选项指定写入关注点的时间限制(毫秒)。wtimeout is only applicable for w values greater than 1.仅适用于大于1w值。

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 wtimeout time limit.当这些写入操作返回时,MongoDB不会撤消在写入问题超过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 w: majority. 从MongoDB 5.0开始,隐式默认写入关注点w: majorityHowever, 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:例如,考虑以下部署及其各自的默认写入问题:

Non-Arbiters非仲裁人ArbitersVoting Nodes投票节点Majority of Voting Nodes多数投票节点Implicit Default Write Concern隐式默认写入关注点
2132{ w: 1 }
4153{ 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 { w: 1 }.非仲裁器(2)的数量等于多数投票节点(2),导致隐式写关注{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 { w: "majority" }.非仲裁器(4)的数量大于多数投票节点(3),导致隐式写关注{ 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 is unspecified未指定j:truej:false
w: 1In memory在内存中On-disk journal磁盘日志In memory在内存中
w: "majority"On-disk journal if running with journaling如果使用日志记录运行,则使用磁盘日志On-disk journal磁盘日志磁盘日志In memory
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"写入操作可能会在给定副本集中的大多数节点发生瞬时丢失(如崩溃和重新启动)时回滚。

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: trueAcknowledgment requires writing operation to on-disk journal.确认需要将操作写入磁盘日志。
j: falseAcknowledgment requires writing operation in memory.确认需要在内存中进行写入操作。
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 is unspecified未指定Acknowledgment requires writing operation in memory (j: false).确认需要在内存中进行写入操作(j:false)。
j: trueAcknowledgment requires writing operation to on-disk journal.确认需要将操作写入磁盘日志。
j: falseAcknowledgment requires writing operation in memory.确认需要在内存中进行写入操作。
Note注意

Hidden, delayed, and priority 0 members can acknowledge w: <number> write operations.隐藏延迟优先级为0的成员可以确认w: <number>写入操作。

Delayed secondaries can return write acknowledgment no earlier than the configured 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行为

Write Concern not Supported on local Databaselocal数据库不支持写问题

The local database does not support write concerns. local数据库不支持写关注点。MongoDB silently ignores any configured write concern for an operation on a collection in the local database.MongoDB静默地忽略本地数据库中集合上的操作的任何已配置写入关注点。

Calculating Majority for Write Concern计算写入关注点的多数

Tip提示

Starting in version 4.2.1, the rs.status() returns the writeMajorityCount field which contains the calculated majority number.从版本4.2.1开始,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.所有有表决权的成员(包括仲裁员)的多数反对。
  • the number of all data-bearing voting members.所有数据承载投票成员的数量。
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:例如,考虑:

  • A replica set with 3 voting members, Primary-Secondary-Secondary (P-S-S):具有3个投票成员的副本集,主要(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个投票成员的副本集,主-次仲裁器(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 "majority" to the client.计算出的多数是2,最小值为2和2。由于写入只能应用于数据承载成员,因此写入必须传播到主成员和次成员,以向客户端确认写入问题"majority"
    Tip提示

    Avoid using 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. 避免在a(P-S-a)或其他拓扑中使用"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)。

Warning警告

Avoid deploying more than one arbiter in a replica set. 避免在副本集中部署多个仲裁器See Concerns with Multiple Arbiters.请参阅与多仲裁器有关的问题

To add an arbiter to an existing replica set:要将仲裁器添加到现有副本集,请执行以下操作:

You do not need to change the cluster wide write concern before starting a new replica set with an arbiter.在使用仲裁器启动新的副本集之前,无需更改集群范围的写关注点。

Write Concern Provenance写一篇关于出处的文章

Starting in version 4.4, MongoDB tracks write concern provenance, which indicates the source of a particular write concern. 从版本4.4开始,MongoDB跟踪写关注点的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值及其重要性:

Provenance来源Description描述
clientSuppliedThe write concern was specified in the application.应用程序中指定了写入问题。
customDefaultThe write concern originated from a custom defined default value. 写入关注点源自自定义定义的默认值。See setDefaultRWConcern.请参阅setDefaultRWConcern
getLastErrorDefaultsThe write concern originated from the replica set's settings.getLastErrorDefaults field.写入问题源自复制集的settings.getLastErrorDefaults字段。
implicitDefaultThe write concern originated from the server in absence of all other write concern specifications.在没有所有其他写关注规范的情况下,写关注源于服务器。
←  Read Concern "snapshot"MongoDB CRUD Concepts →