Database Manual / Reference / Database Commands / Administration

setUserWriteBlockMode (database command数据库命令)

Definition定义

setUserWriteBlockMode

New in version 6.0.在版本6.0中新增。

The setUserWriteBlockMode command blocks and unblocks writes to the entire cluster.setUserWriteBlockMode命令阻止和取消阻止对整个集群的写入。

During cluster-to-cluster sync, mongosync, the cluster-to-cluster synchronization tool, uses the setUserWriteBlockMode command to block writes on the destination cluster. 在集群间同步过程中,集群间同步工具mongosync使用setUserWriteBlockMode命令阻止目标集群上的写入。For more information, see the HTTP API start command.有关更多信息,请参阅HTTP APIstart命令。

If you already blocked writes on a replica set, subsequent calls to setUserWriteBlockMode with global: true fail with an IllegalOperation error if the specified reason does not match the reason you provided when you initially enabled write-blocking. 如果您已经阻止了对副本集的写入,如果指定的原因与您最初启用写入阻止时提供的原因不匹配,则后续使用global:true调用setUserWriteBlockMode将失败,并出现IllegalOperation错误。The error message includes both the current reason and the reason specified in the failed command. Sharded clusters do not enforce this constraint.错误消息包括当前原因和失败命令中指定的原因。分片化集群不会强制执行此约束。

Note

Users and applications with the bypassWriteBlockingMode privilege can bypass the block and continue to perform writes.具有bypassWriteBlockingMode权限的用户和应用程序可以绕过该块并继续执行写入。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
setUserWriteBlockMode: 1,
global: <boolean>,
reason: <string> // Optional
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
setUserWriteBlockModeinteger整数Set this field to 1.将此字段设置为1
globalboolean布尔值Blocks writes on a cluster when set to true. To enable writes on a cluster, set global: false.当设置为true时,块会在集群上写入。要在集群上启用写入,请设置global:false
reasonstring字符串

Optional. 可选。Specifies the reason for blocking writes. Accepts the following values:指定阻止写入的原因。接受以下值:

  • "Unspecified" - Default when you do not provide any reason.默认情况下,您不提供任何理由。
  • "ClusterToClusterMigrationInProgress" - Indicates blocked writes due to ongoing cluster-to-cluster migration.表示由于正在进行的群集到群集迁移而阻止写入。
  • "DiskUseThresholdExceeded" - Indicates blocked writes because disk usage has exceeded a threshold.表示由于磁盘使用率超过阈值而阻止写入。

Required Access所需访问权限

To execute the setUserWriteBlockMode command, the user must have the setUserWriteBlockMode privilege.要执行setUserWriteBlockMode命令,用户必须具有setUserWriteBlockMode权限。

Example示例

  1. Enable user write block mode:启用用户写块模式:

    db.adminCommand( {
    setUserWriteBlockMode: 1,
    global: true
    } )
  2. Add a record to the collection:向集合中添加记录:

    db.names.insertOne( { name: "George Washington Cable" } )

    The server blocks the write because the user write block is enabled.服务器阻止写入,因为用户写入阻止已启用。

    Example Output:输出示例:

    MongoServerError: User writes blocked
  3. Disable user write block mode:禁用用户写块模式:

    db.adminCommand( {
    setUserWriteBlockMode: 1,
    global: false
    } )
  4. Add a record to the collection:向集合中添加记录:

    db.names.insertOne( { name: "George Washington Cable" } )

    The insertOne() method writes to a collection. The server allows the write because the user write block is disabled.insertOne()方法写入集合。服务器允许写入,因为用户写入块已禁用。