On this page本页内容
Write concern for replica sets describe the number of data-bearing members (i.e. the primary and secondaries, but not arbiters) that must acknowledge a write operation before the operation returns as successful. 副本集的写入关注点描述了在操作返回成功之前必须确认写入操作的数据承载成员(即主成员和辅助成员,但不是仲裁器)的数量。A member can only acknowledge a write operation after it has received and applied the write successfully.成员只有在成功接收并应用写入操作后才能确认写入操作。
For replica sets, the write concern of 对于副本集,w: "majority"
requires acknowledgement that the write operations have propagated to a calculated majority of the data-bearing voting members. w: "majority"
的写入问题需要确认写入操作已传播到计算出的大多数数据承载投票成员。For most replica set configurations, 对于大多数副本集配置,w: "majority"
is the default write concern. w: "majority"
是默认的写入问题。To learn how MongoDB determines the default write concern, see Implicit Default Write Concern.要了解MongoDB如何确定默认写问题,请参阅隐式默认写问题。
For clusters where members have journaling enabled, combining 对于成员已启用日志记录的集群,将"majority"
write concern with j : true
can prevent rollback of write concern acknowledged data."majority"
写关注点与j : true
组合可以防止写关注点确认数据的回滚。
Write operations with a write concern of 写问题为w: 1
require that only the primary replica set member acknowledge the write before returning write concern acknowledgment. w: 1
的写操作要求只有主副本集成员在返回写问题确认之前确认写。You can specify an integer value greater than 您可以指定一个大于1
to require acknowledgment from the primary and as many secondaries as needed to meet the specified value, up to the total number of data-bearing members in the replica set.1
的整数值,以要求主服务器和所需数量的辅助服务器进行确认,以满足指定的值,最大值为副本集中数据承载成员的总数。
For complete documentation on write acknowledgment behavior, see Acknowledgment Behavior.有关写入确认行为的完整文档,请参阅确认行为。
An application that issues a write operation that requires write concern acknowledgment waits until the primary receives acknowledgment from the required number of members for the specified write concern. 发出需要写问题确认的写操作的应用程序将等待,直到主应用程序收到指定写问题所需成员数的确认。For write concern of 对于w
greater than 1 or w : "majority"
, the primary waits until the required number of secondaries acknowledge the write before returning write concern acknowledgment. w
大于1或w : "majority"
的写入问题,主系统等待直到所需数量的辅助系统确认写入,然后返回写入问题确认。For write concern of 对于w: 1
, the primary can return write concern acknowledgment as soon as it locally applies the write since it is eligible for contributing to the requested write concern.w: 1
的写入关注点,主服务器可以在本地应用写入时立即返回写入关注点确认,因为它有资格参与请求的写入关注。
The more members that acknowledge a write, the less likely the written data could roll back if the primary fails. 确认写入的成员越多,如果primary
失败,写入的数据回滚的可能性就越小。However, specifying a high write concern can increase latency as the client must wait until it receives the requested level of write concern acknowledgment.但是,指定高写问题可能会增加延迟,因为客户端必须等待,直到收到请求的写问题确认级别。
Selecting the ideal write concern for any given write operation depends on your application's performance goals and data durability requirements. 为任何给定的写入操作选择理想的写入关注点取决于应用程序的性能目标和数据持久性要求。For more guidance on configuring write concern to prevent rollbacks, see Avoid Replica Set Rollbacks.有关配置写问题以防止回滚的更多指导,请参阅避免副本集回滚。
The following operation includes the 以下操作包括writeConcern
option for the insertOne()
method. insertOne()
方法的writeConcern
选项。The operation specifies: - the 该操作指定:-"majority"
write concern, and - a 5 second timeout."majority"
写入关注点,以及5秒超时。
The wtimeout
write concern parameter ensures that the operation does not block indefinitely.wtimeout
写入问题参数确保操作不会无限期阻塞。
db.products.insertOne( { item: "envelopes", qty : 100, type: "Clasp" }, { writeConcern: { w: "majority" , wtimeout: 5000 } } )
The application waits until the primary returns write concern acknowledgment, indicating that a calculated majority of the data-bearing voting members acknowledged the write operation. 应用程序等待,直到主服务器返回写问题确认,表明计算出的大多数数据承载投票成员都确认了写操作。For example, in a 3-member replica set (P-S-S), the operation would require acknowledgment from 2 out of the 3 members. 例如,在一个3成员复制集(P-S-S)中,该操作需要3个成员中的2个成员的确认。If the replica set was later scaled to include two additional voting secondary members, the same operation would require acknowledgment from 3 out of the 5 replica set members. 如果复制集后来被扩展为包括两个额外的投票辅助成员,则相同的操作将需要5个复制集成员中的3个成员的确认。If the primary does not return write concern acknowledgment within the 如果主服务器未在wtimeout
limit, the write operation fails with a write concern error.wtimeout
限制内返回写问题确认,则写操作将失败,并出现写问题错误。
A write operation that times out waiting for the specified write concern only indicates that the required number of replica set members did not acknowledge the write operation within the 等待指定写入问题超时的写入操作仅表示所需数量的副本集成员在wtimeout
time period. wtimeout
时间段内未确认写入操作。It does not necessarily indicate that the primary failed to apply the write. 它不一定表示主应用程序未能应用写入。The data may exist on a subset of replica set nodes at the time of the write concern error, and can continue replicating until all nodes in the cluster have that data. 在发生写问题错误时,数据可能存在于副本集节点的子集上,并且可以继续复制,直到群集中的所有节点都具有该数据。Applications should take into account the potential availability of written data regardless of the state of write concern acknowledgment.应用程序应考虑写入数据的潜在可用性,而不管写入问题确认的状态如何。
The exact syntax for specifying write concern depends on the write operation. 指定写关注点的确切语法取决于写操作。Refer to the documentation for the write operation for instructions on write concern support and syntax. 有关写问题支持和语法的说明,请参阅写操作的文档。For complete documentation on write concern, see Write Concern.有关写问题的完整文档,请参阅写入关注。
You can modify the default write concern for a replica set by issuing the 您可以通过发出setDefaultRWConcern
command.setDefaultRWConcern
命令来修改复制集的默认写入问题。
If you issue a write operation with a specific write concern, the write operation uses its own write concern instead of the default.如果发出具有特定写入关注点的写入操作,则该写入操作将使用其自己的写入关注点而不是默认值。
You can tag the members of replica sets and use the resulting tag sets to create custom write concerns. 您可以标记副本集的成员,并使用生成的标记集创建自定义写入问题。See Configure Replica Set Tag Sets for information on configuring custom write concerns using tag sets.有关使用标记集配置自定义写入问题的信息,请参阅配置副本集标记集。