Definition定义
setDefaultRWConcernThesetDefaultRWConcernadministrative command sets the global default read or write concern configuration for a replica set or sharded cluster.setDefaultRWConcern管理命令为副本集或分片集群设置全局默认读或写入关注配置。setDefaultRWConcernmust be run against the必须针对admindatabase.admin数据库运行。For replica sets, issue the对于副本集,在primarysetDefaultRWConcerncommand on the primarymongod.mongod上发出setDefaultRWConcern命令。For sharded clusters, issue the对于分片集群,在setDefaultRWConcernon amongos.mongos上发出setDefaultRWConcern。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.adminCommand(
{
setDefaultRWConcern : 1,
defaultReadConcern: { <read concern> },
defaultWriteConcern: { <write concern> },
writeConcern: { <write concern> },
comment: <any>
}
)Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
setDefaultRWConcern | int |
|
defaultReadConcern | object |
|
defaultWriteConcern | object |
|
writeConcern | object |
|
comment |
|
setDefaultRWConcern returns an object that contains the currently configured global default read and write concern. 返回一个包含当前配置的全局默认读写入关注的对象。See 有关返回字段的更完整文档,请参阅getDefaultRWConcern for more complete documentation on the returned fields.getDefaultRWConcern。
Behavior行为
Note
Requires featureCompatibilityVersion 4.4+需要功能兼容性版本4.4+
Each 副本集或分片集群中的每个mongod in the replica set or sharded cluster must have featureCompatibilityVersion set to at least 4.4 to use setDefaultRWConcern.mongod必须将featureCompatibilityVersion设置为至少4.4,才能使用setDefaultRWConcern。
Starting in MongoDB 5.0, once the Cluster Wide Write Concern (CWWC) is set via the 从MongoDB 5.0开始,一旦通过setDefaultRWConcern command the write concern cannot be unset.setDefaultRWConcern命令设置了集群范围写入关注(CWWC),就不能取消设置写入关注。
MongoDB only applies the global default read or write concern to operations which do not explicitly specify a read or write concern.MongoDB只将全局默认读或写关注应用于未明确指定读或写入关注的操作。
If MongoDB applies the global default read or write concern to an operation, that operation behaves as if that read or write concern were explicitly specified by the issuing client.如果MongoDB将全局默认读或写关注应用于操作,则该操作的行为就像该读或写入关注是由发出客户端明确指定的一样。
Replica Sets副本集
Issue 对primary副本集发出setDefaultRWConcern against the replica set primary. setDefaultRWConcern。The primary replicates the new global default settings to the remaining members of the replica set. Secondaries which have not yet replicated the updated global default settings continue using their local 'stale' copy of the defaults.主服务器将新的全局默认设置复制到副本集的其余成员。尚未复制更新的全局默认设置的借调人员继续使用其本地默认设置的“过时”副本。
Issue the 发出setDefaultRWConcern command with a writeConcern of w : "majority" to ensure the command only returns after the changes have propagated to a majority of replica set members.writeConcern为w : "majority"的setDefaultRWConcern命令,以确保该命令仅在更改传播到大多数副本集成员后返回。
Sharded Clusters分片集群
Issue the 对集群中的setDefaultRWConcern against a mongos in the cluster. mongos发出setDefaultRWConcern。The mongos persists the updated settings to the config server replica set (CSRS). mongos将更新的设置持久化到配置服务器副本集(CSRS)。Each 每个mongo都会定期向CSRS发出一个mongos periodically issues a getDefaultRWConcern against the CSRS to refresh their local copy of the global settings. getDefaultRWConcern,以刷新其全局设置的本地副本。A mongos uses its local 'stale' copy of the global defaults during the time period between refreshes.mongos在刷新之间的时间段内使用其本地全局默认值的“过时”副本。
Issue the 发出带有setDefaultRWConcern command with a writeConcern of w : "majority" to ensure the command only returns after the changes have propagated to a majority of CSRS members.w : "majority"的setDefaultRWConcern命令,以确保该命令仅在更改传播到大多数CSRS成员后返回。
When an application issues an operation against the 当应用程序在没有明确指定读或写关注设置的情况下对mongos without explicitly specifying a read or write concern setting, the mongos applies the corresponding global default setting.mongos发出操作时,mongos会应用相应的全局默认设置。
The global default settings do not propagate to the individual shards. You cannot run 全局默认设置不会传播到单个分片。您不能对分片运行setDefaultRWConcern against a shard.setDefaultRWConcern。
Important
setDefaultRWConcern requires featureCompatibilityVersion 4.4+. setDefaultRWConcern需要功能兼容性版本4.4+。If you downgrade your deployment's featureCompatibilityVersion from 如果将部署的featureCompatibilityVersion从4.4 to 4.2, all cluster-wide read and write concern defaults are lost, but mongos instances may continue applying the defaults for up to 30 seconds.4.4降级到4.2,则所有集群范围的读写入关注默认值都将丢失,但mongos实例可能会继续应用默认值长达30秒。
Sharding Administrative Commands Override Write Concern Settings分片管理命令覆盖写入关注设置
Sharding administrative commands that perform write operations on the config server, such as the 在配置服务器上执行写入操作的分片管理命令,如enableSharding or addShard commands, have specific behavior with global default write concern settings:enableSharding或addShard命令,具有特定的行为和全局默认写入关注设置:
The commands use无论配置的全局默认写入关注如何,这些命令都使用"majority"regardless of the configured global default write concern."majority"。The commands use a minimum wtimeout of这些命令使用的最小60000. The commands only use the global default write concernwtimeoutif it is greater than60000.wtimeout为60000。如果全局默认写入关注wtimeout大于60000,则命令仅使用该值。
Access Control访问控制
For replica sets or sharded clusters enforcing Authentication on Self-Managed Deployments, 对于在自我管理部署上强制执行身份验证的副本集或分片集群,setDefaultRWConcern requires that the authenticated user have the setDefaultRWConcern privilege action.setDefaultRWConcern要求经过身份验证的用户具有setDefaultRWConcern权限操作。
The clusterManager built-in role provides the required privileges to run setDefaultRWConcern.clusterManager内置角色提供运行setDefaultRWConcern所需的权限。
Example示例
Set Global Default Write Concern设置全局默认写入关注
The following operation sets the global write concern to the following:以下操作将全局写入关注设置为以下内容:
db.adminCommand({
"setDefaultRWConcern" : 1,
"defaultWriteConcern" : {
"w" : 2
}
})
The operation returns a document similar to the following:该操作返回一个类似于以下内容的文档:
{
"defaultWriteConcern" : {
"w" : 2
},
"updateOpTime" : Timestamp(1586290895, 1),
"updateWallClockTime" : ISODate("2020-04-07T20:21:41.849Z"),
"localUpdateWallClockTime" : ISODate("2020-04-07T20:21:41.862Z"),
"ok" : 1,
"$clusterTime" : { ... }
"operationTime" : Timestamp(1586290925, 1)
}Set Global Default Read Concern设置全局默认读取关注
The following operation sets the global read concern to 以下操作将全局读取关注设置为"majority":"majority":
db.adminCommand({
"setDefaultRWConcern" : 1,
"defaultReadConcern" : { "level" : "majority" }
})
The operation returns a document similar to the following:该操作返回一个类似于以下内容的文档:
{
"defaultReadConcern" : {
"level" : "majority"
},
"updateOpTime" : Timestamp(1586290895, 1),
"updateWallClockTime" : ISODate("2020-04-07T20:21:41.849Z"),
"localUpdateWallClockTime" : ISODate("2020-04-07T20:21:41.862Z"),
"ok" : 1,
"$clusterTime" : { ... }
"operationTime" : Timestamp(1586290925, 1)
}Set Global Default Read and Write Concern设置全局默认读写入关注
The following operation sets the global default read and write concern to the following:以下操作将全局默认读写入关注设置为以下内容:
w: 2write concernlevel: "majority"read concern.
db.adminCommand({
"setDefaultRWConcern" : 1,
"defaultWriteConcern" : {
"w" : 2
},
"defaultReadConcern" : { "level" : "majority" }
})
The operation returns a document similar to the following:该操作返回一个类似于以下内容的文档:
"defaultWriteConcern" : {
"w" : 2
},
"defaultReadConcern" : {
"level" : "majority"
}Unset Global Default Read and Write Concern取消设置全局默认读写入关注
You can:你可以:
Unset the global default read concern.取消设置全局默认读取关注。Only unset the global default write concern if you haven't already set it.只有当您尚未设置全局默认写入关注时,才取消设置它。
For example, assume the global default read concern is set to 例如,假设全局默认读取关注设置为level: "majority". To unset the global default read concern, use an empty document {}:level: "majority"。要取消设置全局默认读取关注,请使用空文档{}:
db.adminCommand( {
"setDefaultRWConcern" : 1,
"defaultReadConcern" : {}
} )
The operation returns a document that indicates the operation was successful:该操作返回一个文档,指示操作成功:
{
defaultReadConcern: { level: 'local' },
defaultWriteConcern: { w: 2, wtimeout: 0 },
updateOpTime: Timestamp({ t: 1656696934, i: 1 }),
updateWallClockTime: ISODate("2022-07-01T17:35:40.578Z"),
defaultWriteConcernSource: 'global',
defaultReadConcernSource: 'implicit',
localUpdateWallClockTime: ISODate("2022-07-01T17:35:40.578Z"),
ok: 1,
'$clusterTime': {
...
},
operationTime: Timestamp({ t: 1656632593, i: 1 })
}
You can only unset the global default write concern if you haven't already set it.如果尚未设置全局默认写入关注,则只能取消设置它。
To unset the global default write concern, use an empty document 要取消设置全局默认写入关注,请使用空文档{}:{}:
db.adminCommand( {
"setDefaultRWConcern" : 1,
"defaultWriteConcern" : {}
} )
If the global default write concern is:如果全局默认写入关注是:
Unset, the operation succeeds.取消设置,操作成功。Already set, the operation returns the following error.已设置,操作返回以下错误。
MongoServerError: The global default write concern cannot be unset
once it is set.