定義
enableShardingThe
enableShardingcommand explicitly creates a database.Tip
In
mongosh, this command can also be run through thesh.enableSharding()helper method.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.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
このコマンドの構文は、次のとおりです。
推奨。 作成するデータベースを指定します。
db.adminCommand( { enableSharding: "<database name>" } ) オプションで、 コマンドにプライマリシャード仕様を含めることができますが、これは推奨されません。
db.adminCommand( { enableSharding: "<database name>", primaryShard: "<shard name>" } )
You can only run the enableSharding command on the admin database from a mongos instance.
コマンドフィールド
このコマンドは、次のフィールドを使用します。
フィールド | タイプ | 説明 |
|---|---|---|
文字列 | 使用するデータベース。 データベースが存在しない場合は作成されます。 | |
文字列 | 任意。 データベースのプライマリシャード。プライマリシャードには、このデータベースのシャーディングされていないコレクションが含まれています。 一般に、プライマリ シャードを明示的に指定するのではなく、バランサーによってプライマリ シャードが選択されるようにすることをお勧めします。 警告 Tip 一般に、プライマリシャードを指定する必要はありません。 バランサーが代わりにプライマリ シャードを選択できるようにします。 |
戻り値
このコマンドは、操作のステータスを含むドキュメントを返します。
動作
書込み保証 (write concern)
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 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. To change the primary shard for a database, use movePrimary instead.
例
mongosから次のコマンドを実行すると、 shardTestデータベースが作成されます。
db.adminCommand( { enableSharding: "shardTest" } )