定义
sh.enableSharding(database, primaryShard)Explicitly creates a database. Use the
mongoshmethodsh.shardCollection()to shard collections on the database. Themongoshmethodsh.enableSharding()wraps theenableShardingcommand.重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.有关数据库命令,请参阅
enableSharding命令。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
重要
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
The sh.enableSharding() has the following form:
sh.enableSharding( <database>, <primary shard> // Optional. )
Parameter
The sh.enableSharding() method takes the following parameter:
行为
写关注
mongos uses "majority" for the enableSharding command and its helper sh.enableSharding().
主分片
In general, you should not need to specify the primaryShard in the command. Instead, allow the balancer to select the primary shard.
However, if you do specify the primary shard for a database and the database is already enabled for sharding with a different primary shard, the operation returns an error and the primary shard for the database remains as before. To change the primary shard for a database, use the movePrimary command instead.
例子
下面的示例从 mongos 运行,明确创建了 shardTest 数据库:
sh.enableSharding("shardTest")
要验证,请运行 sh.status()。