On this page本页内容
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
command on the primary mongod
.mongod
上发出setDefaultRWConcern
命令。setDefaultRWConcern
on a mongos
.mongos
上发出setDefaultRWConcern
。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:具有以下字段:
setDefaultRWConcern | int | Set to |
defaultReadConcern | object |
|
defaultWriteConcern | object |
|
writeConcern | object |
|
comment | any |
|
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
。
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将全局默认读或写关注点应用于某个操作,那么该操作的行为就好像该读或写关心点是由发出请求的客户端明确指定的一样。
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 发出writeConcern为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.w : "majority"
的setDefaultRWConcern
命令,以确保该命令仅在更改传播到大多数副本集成员后返回。
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 每个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"
的writeConcern
的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
。
setDefaultRWConcern
requires featureCompatibilityVersion 4.4+
. setDefaultRWConcern
需要featureCompatibilityVersion 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 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
命令)具有全局默认写问题设置的特定行为:
"majority"
regardless of the configured global default write concern."majority"
。60000
. 60000
。wtimeout
if it is greater than 60000
.wtimeout
大于60000
时使用。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
所需的权限。
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) }
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) }
The following operation sets the global default read and write concern to the following:以下操作将全局默认读写问题设置为:
w: 2
level: "majority"
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" }
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) }