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
must be run against the admin
database.
setDefaultRWConcern
command on the primary mongod
.setDefaultRWConcern
on a mongos
.setDefaultRWConcern
has the following form:
setDefaultRWConcern
has the following fields:
setDefaultRWConcern | int |
Set to |
defaultReadConcern | object |
Document containing the global read concern configuration. Specify a valid read concern object.
Omit this document to leave the current global read concern unmodified. If omitted, |
defaultWriteConcern | object |
Document containing the global default write concern configuration.
Omit this document to leave the current global write concern unmodified. If omitted, |
writeConcern | object | Optional. A document that specifies the write concern to be used by the If omitted, |
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
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.
Requires featureCompatibilityVersion
4.4+
Each mongod
in the replica set or sharded cluster must have featureCompatibilityVersion set to at least 4.4
to use setDefaultRWConcern
.
MongoDB only applies the global default read or write concern to operations which do not explicitly specify a read or write concern.
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.
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.
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.
The global default settings do not propagate to the individual shards. You cannot run setDefaultRWConcern
against a shard.
Important
setDefaultRWConcern
requires 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.
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:
"majority"
regardless of the configured global default write concern.60000
. The commands only use the global default write concern wtimeout
if it is greater than 60000
.For replica sets or sharded clusters enforcing Authentication, setDefaultRWConcern
requires that the authenticated user have the setDefaultRWConcern
privilege action.
The clusterManager
built-in role provides the required privileges to run setDefaultRWConcern
.
The following operation sets the global write concern to the following:
The operation returns a document similar to the following:
The following operation sets the global read concern to "majority"
:
The operation returns a document similar to the following:
The following operation sets the global default read and write concern to the following:
w: "majority"
write concernlevel: "majority"
read concern.The operation returns a document similar to the following:
Consider a deployment with the following default read and write concern:
The following operation unsets the global default read and write concern settings:
The operation returns a document similar to the following: