Definition定义
enableSharding-
Note
TheenableShardingcommand explicitly creates a database.enableSharding命令显式创建数据库。Tip
In在mongosh, this command can also be run through thesh.enableSharding()helper method.mongosh中,此命令也可以通过sh.enableSharding()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用数据库命令。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The command has the following syntax:该命令具有以下语法:
Recommended. Specify the database to create:推荐。指定要创建的数据库:db.adminCommand(
{
enableSharding: "<database name>"
}
)Optionally, you can include the primary shard specification in the command, although this is not recommended:您可以选择在命令中包含主分片规范,但不建议这样做:db.adminCommand(
{
enableSharding: "<database name>",
primaryShard: "<shard name>"
}
)
You can only run the 您只能从enableSharding command on the admin database from a mongos instance.mongos实例在admin数据库上运行enableSharding命令。
Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
enableSharding |
| |
primaryShard |
|
Returns值
The command returns a document that contains status of the operation.该命令返回一个包含操作状态的文档。
Behavior行为
Write Concern写关注
mongos uses "majority" for the enableSharding command and its helper sh.enableSharding().mongos在enableSharding命令及其辅助函数shenableSharding()中使用"majority"。
Primary Shard主分片
In general, you should not need to specify the primaryShard in the command. Instead, allow the balancer to select the primary shard.一般来说,您不需要在命令中指定primaryShard。相反,允许平衡器选择主分片。
However, if you do specify the primaryShard in the command for a database and the database is already sharding enabled with a different primary shard, the operation returns an error and the primary shard for the database remains as before. 但是,如果您在数据库的命令中指定了primaryShard,并且数据库已经使用不同的主分片启用了分片,则操作将返回错误,数据库的主分片将保持不变。To change the primary shard for a database, use 要更改数据库的主分片,请改用movePrimary instead.movePrimary。
Example示例
The following command, run from a 以下命令从mongos, creates the the shardTest database:mongos运行,创建shardTest数据库:
db.adminCommand( { enableSharding: "shardTest" } )