定义
setDefaultRWConcernThe
setDefaultRWConcernadministrative command sets the global default read or write concern configuration for a replica set or sharded cluster.setDefaultRWConcernmust be run against theadmindatabase.For replica sets, issue the
setDefaultRWConcerncommand on the primarymongod.For sharded clusters, issue the
setDefaultRWConcernon amongos.
兼容性
此命令可用于以下环境中托管的部署:
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.adminCommand( { setDefaultRWConcern : 1, defaultReadConcern: { <read concern> }, defaultWriteConcern: { <write concern> }, writeConcern: { <write concern> }, comment: <any> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 设置为 | |
| 包含全局读关注配置的文档。指定一个有效的读关注对象。
Omit this document to leave the current global read concern unmodified. If omitted, | |
| 包含全局默认写关注配置的文档。
Omit this document to leave the current global write concern unmodified. If omitted, | |
对象 | Optional. A document that specifies the write concern to be used by the If omitted, | |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
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.
行为
注意
需要 featureCompatibilityVersion 4.4+
Each mongod in the replica set or sharded cluster must have featureCompatibilityVersion set to at least 4.4 to use setDefaultRWConcern.
从MongoDB5.0 开始,一旦通过 命令设立了集群范围的写关注(writesetDefaultRWConcern concern)关注 (CWWC),就无法取消设置。
MongoDB 仅将全局默认读关注或写关注应用于未显式指定读关注或写关注的操作。
如果 MongoDB 将全局默认的读关注或写关注应用于某个操作,则该操作的行为就像该读关注或写关注是由发出客户端显式指定的一样。
副本集
Issue setDefaultRWConcern against the replica set primary. 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.
分片集群
Issue the setDefaultRWConcern against a mongos in the cluster. The mongos persists the updated settings to the config server replica set (CSRS). Each mongos periodically issues a getDefaultRWConcern against the CSRS to refresh their local copy of the global settings. A mongos uses its local 'stale' copy of the global defaults during the time period between refreshes.
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.
当应用程序在未明确指定读关注或写关注设置的情况下针对 mongos 执行操作时,mongos 将应用相应的全局默认设置。
The global default settings do not propagate to the individual shards. You cannot run setDefaultRWConcern against a shard.
重要
setDefaultRWConcern需要 featureCompatibilityVersion4.4+ 。如果您将部署的 featureCompatibilityVersion 从4.4 降级为4.2 ,则所有集群范围的读关注和写关注(write concern)默认值都将丢失,但mongos 实例可能会继续应用默认值长达30 秒。
分片管理命令覆盖写关注设置
在配置服务器上执行写入操作的分片管理命令,例如 enableSharding 或 addShard 命令,使用全局默认写关注设置时具有特定行为:
无论配置何种全局默认写关注,这些命令都使用
"majority"。这些命令使用的最小 wtimeout 为
60000。仅当全局默认写关注大于60000时,这些命令才使用全局默认写关注wtimeout。
访问控制
For replica sets or sharded clusters enforcing Authentication on Self-Managed Deployments, setDefaultRWConcern requires that the authenticated user have the setDefaultRWConcern privilege action.
The clusterManager built-in role provides the required privileges to run setDefaultRWConcern.
例子
设置全局默认写关注
以下操作将全局写关注设置为:
db.adminCommand({ "setDefaultRWConcern" : 1, "defaultWriteConcern" : { "w" : 2 } })
该操作返回类似于以下内容的文档:
{ "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) }
设置全局默认读关注
以下操作将全局读关注设置为 "majority":
db.adminCommand({ "setDefaultRWConcern" : 1, "defaultReadConcern" : { "level" : "majority" } })
该操作返回类似于以下内容的文档:
{ "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) }
取消设置全局默认读关注和写关注
以下操作将全局默认读关注和写关注设置为:
w: 2写入安全机制level: "majority"读关注。
db.adminCommand({ "setDefaultRWConcern" : 1, "defaultWriteConcern" : { "w" : 2 }, "defaultReadConcern" : { "level" : "majority" } })
该操作返回类似于以下内容的文档:
"defaultWriteConcern" : { "w" : 2 }, "defaultReadConcern" : { "level" : "majority" }
设置全局默认读关注和写关注
您可以:
取消设置全局默认读关注。
仅当尚未设置全局默认写关注时,才能取消设置。
例如,假设全局默认读关注设置为 level: "majority"。要取消设置全局默认读关注,请使用空文档 {}:
db.adminCommand( { "setDefaultRWConcern" : 1, "defaultReadConcern" : {} } )
该操作返回一个文档,指明操作成功:
{ 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 }) }
仅当尚未设置全局默认写关注时才能取消设置。
要取消设置全局默认写关注,请使用空文档 {}:
db.adminCommand( { "setDefaultRWConcern" : 1, "defaultWriteConcern" : {} } )
如果全局默认写关注为:
取消设置,操作成功。
已设置,操作将返回以下错误。
MongoServerError: The global default write concern cannot be unset once it is set.