setDefaultRWConcern

On this page本页内容

Definition定义

New in version 4.4.在版本4.4中新增

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数据库运行。

setDefaultRWConcern has the following form:具有以下形式:

db.adminCommand(
  {
    setDefaultRWConcern : 1,
    defaultReadConcern: { <read concern> },
    defaultWriteConcern: { <write concern> },
    writeConcern: { <write concern> },
    comment: <any>
  }
)

setDefaultRWConcern has the following fields:具有以下字段:

Field字段Type类型Description描述
setDefaultRWConcernint

Set to 1.

defaultReadConcernobject

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

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

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

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类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

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"writeConcernsetDefaultRWConcern命令,以确保该命令仅在更改传播到大多数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需要featureCompatibilityVersion 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. 这些命令使用的最小wtimeout60000The commands only use the global default write concern wtimeout if it is greater than 60000.这些命令仅在全局默认写入问题wtimeout大于60000时使用。

Access Control访问控制

For replica sets or sharded clusters enforcing Authentication, 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未设置全局默认读写问题

Consider a deployment with the following default read and write concern:考虑具有以下默认读写问题的部署:

{
  "defaultWriteConcern" : {
    "w" : "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)
}

The following operation unsets the global default read and write concern settings:以下操作将取消设置全局默认读写问题设置:

db.adminCommand({
  "setDefaultRWConcern" : 1,
  "defaultReadConcern" : {},
  "defaultWriteConcern" : {}
})

The operation returns a document similar to the following:该操作返回一个类似于以下内容的文档:

{
  "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)
}
←  setParametershutdown →