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

setUserWriteBlockMode(数据库命令)

setUserWriteBlockMode

6.0版本新增。

setUserWriteBlockMode命令会阻止和取消阻止对整个集群的写入。

During cluster synchronization, mongosync uses the setUserWriteBlockMode command to block writes on both the source and destination clusters. For more information, see the HTTP API start command.

如果您已经阻止了对副本集的写入,则如果指定的 reason 与您最初启用写入阻止时提供的原因不匹配,则后续使用 global: true 调用 setUserWriteBlockMode 会失败并显示 IllegalOperation 错误。错误消息包括当前原因和失败命令中指定的原因。分片集群不会实施此约束。

注意

具有bypassWriteBlockingMode特权的用户和应用程序可以绕过该阻止并继续执行写入。

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

重要

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.

该命令具有以下语法:

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

该命令接受以下字段:

字段
类型
说明

setUserWriteBlockMode

整型

将此字段设置为1 。

global

布尔

设置为true时阻止写入集群。 要启用对集群的写入,请设置global: false 。

reason

字符串

可选。指定阻止写入的原因。接受以下值:

  • "Unspecified" — 未提供任何理由时的默认值。

  • "ClusterToClusterMigrationInProgress" — 表示由于正在进行的 mongosync迁移导致写入受阻。

  • "DiskUseThresholdExceeded" — 表示由于磁盘使用率已超过阈值而导致写入受阻。

要执行setUserWriteBlockMode命令,用户必须具有setUserWriteBlockMode特权。

  1. 启用用户写入阻止模式:

    db.adminCommand( {
    setUserWriteBlockMode: 1,
    global: true
    } )
  2. 向collection添加一条记录:

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

    服务器阻止写入,因为已启用用户写入阻止。

    示例输出:

    MongoServerError: User writes blocked
  3. 禁用用户写入块模式:

    db.adminCommand( {
    setUserWriteBlockMode: 1,
    global: false
    } )
  4. 向collection添加一条记录:

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

    insertOne()方法可写入集合。 服务器允许写入,因为用户写入块已禁用。