Definition定义
setUserWriteBlockModeNew in version 6.0.在版本6.0中新增。ThesetUserWriteBlockModecommand blocks and unblocks writes to the entire cluster.setUserWriteBlockMode命令阻止和取消阻止对整个集群的写入。During cluster-to-cluster sync,在集群间同步过程中,集群间同步工具mongosync, the cluster-to-cluster synchronization tool, uses thesetUserWriteBlockModecommand 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如果您已经阻止了对副本集的写入,如果指定的原因与您最初启用写入阻止时提供的原因不匹配,则后续使用setUserWriteBlockModewithglobal: truefail with anIllegalOperationerror if the specifiedreasondoes 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具有bypassWriteBlockingModeprivilege 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:该命令包含以下字段:
setUserWriteBlockMode | 1.1。 | |
global | true. To enable writes on a cluster, set global: false.true时,块会在集群上写入。要在集群上启用写入,请设置global:false。 | |
reason |
|
Required Access所需访问权限
To execute the 要执行setUserWriteBlockMode command, the user must have the setUserWriteBlockMode privilege.setUserWriteBlockMode命令,用户必须具有setUserWriteBlockMode权限。
Example示例
Enable user write block mode:启用用户写块模式:db.adminCommand( {
setUserWriteBlockMode: 1,
global: true
} )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 blockedDisable user write block mode:禁用用户写块模式:db.adminCommand( {
setUserWriteBlockMode: 1,
global: false
} )Add a record to the collection:向集合中添加记录:db.names.insertOne( { name: "George Washington Cable" } )TheinsertOne()method writes to a collection. The server allows the write because the user write block is disabled.insertOne()方法写入集合。服务器允许写入,因为用户写入块已禁用。