sh.setBalancerState()
On this page
Description
sh.setBalancerState(state)
Enables or disables the balancer. Starting in MongoDB 4.2, the method also enables auto-splitting if enabling the balancer and disables auto-splitting if disabling the balancer.
Use
sh.getBalancerState()
to determine if the balancer is currently enabled or disabled andsh.isBalancerRunning()
to check its current state.Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
balancerStart
andbalancerStop
commands.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
Important
You can only run
sh.setBalancerState()
on amongos
instance.sh.setBalancerState()
errors if run onmongod
instance.The
sh.setBalancerState()
method has the following parameter:ParameterTypeDescriptionstate
boolean
Set this to
true
to enable the balancer andfalse
to disable it.Leaving the balancer disabled for extended periods of time can lead to unbalanced shards, which degrade cluster performance. Only disable the balancer if necessary, and ensure that you re-enable the balancer when maintenance is complete.
Compatibility
This method 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, M2, and M5 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