对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

sh.enableSharding()(mongosh方法)

sh.enableSharding(database, primaryShard)

注意

在版本6.0中进行了更改。

从 MongoDB 6.0 开始,不需要使用此方法来分片集合。

Explicitly creates a database. Use the mongosh method sh.shardCollection() to shard collections on the database. The mongosh method sh.enableSharding() wraps the enableSharding command.

重要

mongosh 方法

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

有关数据库命令,请参阅 enableSharding 命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

重要

M0 和 Flex 集群不支持此命令。有关更多信息,请参阅不支持的命令。

The sh.enableSharding() has the following form:

sh.enableSharding(
<database>,
<primary shard> // Optional.
)

The sh.enableSharding() method takes the following parameter:

Parameter
类型
说明

字符串

要创建的数据库名称。

字符串

可选。数据库的主分片;主分片包含此数据库的未分片集合。一般来说,建议让负载均衡器选择主分片,而不是明确指定主分片。

警告

提示

通常,您不需要指定主分片。允许负载均衡器选择主分片。

mongos uses "majority" for the enableSharding command and its helper sh.enableSharding().

一般来说,不需要在命令中指定 primaryShard。相反,允许负载均衡器选择主分片。

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()

给本页内容打分