Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/
Database Manual
/ / /

setUserWriteBlockMode (database command)

setUserWriteBlockMode

New in version 6.0.

The setUserWriteBlockMode command blocks and unblocks writes to the entire cluster.

During cluster-to-cluster sync, mongosync, the cluster-to-cluster synchronization tool, uses the setUserWriteBlockMode command 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 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. 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.

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.

Important

This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

The command has the following syntax:

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

The command takes the following fields:

Field
Type
Description

setUserWriteBlockMode

integer

Set this field to 1.

global

boolean

Blocks writes on a cluster when set to true. To enable writes on a cluster, set global: false.

reason

string

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.

To execute the setUserWriteBlockMode command, the user must have the setUserWriteBlockMode privilege.

  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.

Back

setQuerySettings

On this page