AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

enableSharding(データベースコマンド)

enableSharding

注意

バージョン6.0で変更。

MongoDB 6.0 以降では、コレクションを シャード するためにこのコマンドは必須ではあり ません 。

The enableSharding command explicitly creates a database.

Tip

In mongosh, this command can also be run through the sh.enableSharding() helper method.

Helper methods are convenient for mongosh users, 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

一般に、プライマリシャードを指定する必要はありません。 バランサーが代わりにプライマリ シャードを選択できるようにします。

このコマンドは、操作のステータスを含むドキュメントを返します。

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" } )