定義
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> } )
コマンドフィールド
このコマンドは、次のフィールドを使用します。
フィールド | タイプ | 説明 |
|---|---|---|
|
| |
| グローバルな読み取り保証 (read concern) の設定を含むドキュメント。有効な読み取り保証オブジェクトを指定します。
Omit this document to leave the current global read concern unmodified. If omitted, | |
| グローバルなデフォルトの書込み保証 (write concern) の構成を含むドキュメント。
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 型(string, integer, object, array など)を使用できます。 |
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 以降では、 コマンドを使用して CWWC(Cluster Wide Write Concern)を設定すると、書込み保証 (writesetDefaultRWConcern concern) ) を設定解除できなくなります。
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書込み保証 (write concern)level: "majority"読み取り保証 (read concern)。
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.