Database Manual / Reference / Database Commands / Administration

setDefaultRWConcern (database command数据库命令)

Definition定义

setDefaultRWConcern

The setDefaultRWConcern administrative command sets the global default read or write concern configuration for a replica set or sharded cluster. setDefaultRWConcern管理命令为副本集或分片集群设置全局默认读或写入关注配置。setDefaultRWConcern must be run against the admin database.必须针对admin数据库运行。

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:该命令包含以下字段:

Field字段Type类型Description描述
setDefaultRWConcernint

Set to 1.设置为1

defaultReadConcernobject

Document containing the global read concern configuration. Specify a valid read concern object.包含全局读取关注配置的文档。指定有效的读取关注对象

  • setDefaultRWConcern supports the following subset of level:支持以下级别子集:

  • To unset the currently configured default read concern, specify an empty document {}.要取消设置当前配置的默认读取关注,请指定一个空文档{}
  • setDefaultRWConcern only supports the levels read concern setting. You cannot specify any other read concern setting in the default read concern.仅支持levels读取关注设置。您不能在默认读取关注中指定任何其他读取关注设置。

Omit this document to leave the current global read concern unmodified.省略此文档,使当前的全局读取关注保持不变。 If omitted, setDefaultRWConcern must specify defaultWriteConcern.如果省略,setDefaultRWConcern必须指定defaultWriteConcern

defaultWriteConcernobject

Document containing the global default write concern configuration.包含全局默认写入关注配置的文档。

  • For the write concern w setting, setDefaultRWConcern supports all write concern values except w : 0.对于写入关注w设置,setDefaultRWConcern支持除w:0之外的所有写入关注值。
  • For the write concern wtimeout setting, setDefaultRWConcern defaults to 0 if the setting is omitted. 对于写入关注wtimeout设置,如果省略该设置,则setDefaultRWConcern默认为0Operations block until the requested write concern is met. If specifying a global default wtimeout, ensure the value is large enough to allow write operations to achieve the requested write concern.操作块,直到满足请求的写入关注。如果指定全局默认wtimeout,请确保该值足够大,以允许写入操作实现请求的写入关注。
  • To unset the currently configured write concern, specify an empty document {}.要取消设置当前配置的写入关注,请指定一个空文档{}

Omit this document to leave the current global write concern unmodified. 省略此文档,使当前的全局写入关注保持不变。If omitted, setDefaultRWConcern must specify defaultReadConcern.如果省略,setDefaultRWConcern必须指定defaultReadConcern

writeConcernobject

Optional. 可选。A document that specifies the write concern to be used by the setDefaultRWConcern command itself.一个文档,指定setDefaultRWConcern命令本身要使用的写入关注

If omitted, setDefaultRWConcern uses the previously set global default write concern if one was configured.如果省略,setDefaultRWConcern将使用之前设置的全局默认写入关注(如果已配置)。

commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的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.有关返回字段的更完整文档,请参阅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 setDefaultRWConcern command the write concern cannot be unset.从MongoDB 5.0开始,一旦通过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 setDefaultRWConcern against the replica set primary. primary副本集发出setDefaultRWConcernThe 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.发出writeConcernw : "majority"setDefaultRWConcern命令,以确保该命令仅在更改传播到大多数副本集成员后返回。

Sharded Clusters分片集群

Issue the setDefaultRWConcern against a mongos in the cluster. 对集群中的mongos发出setDefaultRWConcernThe mongos persists the updated settings to the config server replica set (CSRS). mongos将更新的设置持久化到配置服务器副本集(CSRS)。Each mongos periodically issues a getDefaultRWConcern against the CSRS to refresh their local copy of the global settings. 每个mongo都会定期向CSRS发出一个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 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.如果将部署的featureCompatibilityVersion4.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:配置服务器上执行写入操作的分片管理命令,如enableShardingaddShard命令,具有特定的行为和全局默认写入关注设置:

  • 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 concern wtimeout if it is greater than 60000.这些命令使用的最小wtimeout60000。如果全局默认写入关注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:以下操作将全局默认读写入关注设置为以下内容:

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.