对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

setDefaultRWConcern(数据库命令)

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.

此命令可用于以下环境中托管的部署:

该命令具有以下语法:

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

该命令接受以下字段:

字段
类型
说明

int

设置为 1

object

包含全局读关注配置的文档。指定一个有效的读关注对象

Omit this document to leave the current global read concern unmodified. If omitted, setDefaultRWConcern must specify defaultWriteConcern.

object

包含全局默认写关注配置的文档。

  • For the write concern w setting, setDefaultRWConcern supports all write concern values except w : 0.

  • For the write concern wtimeout setting, setDefaultRWConcern defaults to 0 if the setting is omitted. Operations 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.

  • 要取消设置当前配置的写关注,请指定空文档 {}

Omit this document to leave the current global write concern unmodified. If omitted, setDefaultRWConcern must specify defaultReadConcern.

对象

Optional. A document that specifies the write concern to be used by the setDefaultRWConcern command itself.

If omitted, setDefaultRWConcern uses the previously set global default write concern if one was configured.

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 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.

注意

需要 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 开始,一旦通过 命令设立了集群范围的写关注(writesetDefaultRWConcern concern)关注 (CWWC),就无法取消设置。

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+ 。如果您将部署的 featureCompatibilityVersion4.4 降级为4.2 ,则所有集群范围的读关注和写关注(write concern)默认值都将丢失,但mongos 实例可能会继续应用默认值长达30 秒。

配置服务器上执行写入操作的分片管理命令,例如 enableShardingaddShard 命令,使用全局默认写关注设置时具有特定行为:

  • 无论配置何种全局默认写关注,这些命令都使用 "majority"

  • 这些命令使用的最小 wtimeout60000。仅当全局默认写关注大于 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)
}

以下操作将全局默认读关注和写关注设置为:

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.