Docs HomeMongoDB Manual

sh.setBalancerState()

On this page本页内容

Description描述

sh.setBalancerState(state)

Enables or disables the balancer.启用或禁用平衡器

Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改

In MongoDB versions earlier than 6.1, sh.setBalancerState() also enables auto-splitting if enabling the balancer and disables auto-splitting if disabling the balancer.在6.1之前的MongoDB版本中,如果启用平衡器,sh.setBalancerState()也会启用自动拆分,如果禁用平衡器,则会禁用自动拆分。

Use sh.getBalancerState() to determine if the balancer is currently enabled or disabled and sh.isBalancerRunning() to check its current state.使用sh.getBalancerState()确定平衡器当前是启用还是禁用,并使用sh.isBalancerRunning()检查其当前状态。

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 and balancerStop commands.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

Important

You can only run sh.setBalancerState() on a mongos instance. 您只能在mongos实例上运行sh.setBalancerState()sh.setBalancerState() errors if run on mongod instance.如果在mongod实例上运行,则会出现sh.setBalancerState()错误。

The sh.setBalancerState() method has the following parameter:sh.setBalancerState()方法具有以下参数:

Parameter参数Type类型Description描述
statebooleanSet this to true to enable the balancer and false to disable it.将此设置为true可启用平衡器,将其设置为false可禁用平衡器。