Definition
setUserWriteBlockModeNew in version 6.0.
The
setUserWriteBlockModecommand blocks and unblocks writes to the entire cluster.During cluster-to-cluster sync,
mongosync, the cluster-to-cluster synchronization tool, uses thesetUserWriteBlockModecommand to block writes on the destination cluster. For more information, see the HTTP API start command.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. 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.
Compatibility
This command is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of 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 |
|---|---|---|
| integer | Set this field to |
| boolean | Blocks writes on a cluster when set to |
| string | Optional. Specifies the reason for blocking writes. Accepts the following values:
|
Required Access
To execute the setUserWriteBlockMode command, the user must
have the setUserWriteBlockMode privilege.
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 blocked Disable user write block mode:
db.adminCommand( { setUserWriteBlockMode: 1, global: false } ) 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.