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

创建、查看、删除和对集合进行分片

You can manage the collections in your clusters using the Atlas UI and mongosh.

下表描述了管理 Atlas 项目中集合所需的角色:

操作
所需角色

创建集合

查看集合

至少是 Project Data Access Read Only 角色。

删除集合

碎片集合

以下角色之一:

提示

要在新数据库中创建第一个集合,请参阅创建数据库。

重要

您无法在 config 和 system 数据库上创建新集合。在不久的将来,Atlas 将不再支持向这些数据库上的现有集合写入数据。

要通过 Atlas 用户界面在现有数据库中创建集合:

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。

  2. 如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。

  3. 在侧边栏中,单击 Database 标题下的 Data Explorer。

    The Data Explorer displays.

2

Select or hover over the database, and click the icon to open the Create Collection dialog box.

3

在 Create Collection 对话框中,输入要创建的集合的名称。

Atlas还提供 Additional preferences。您可以从以下选项中进行选择:

重要

请勿在集合名称中包含敏感信息。

For more information on MongoDB collection names, see Naming Restrictions.

4

Select whether the collection is a time series collection. If you select to create a time series collection, specify the time field and granularity. You can optionally specify the meta field and the time for old data in the collection to expire.

5

成功创建后,该集合会显示在 Connections 侧边栏中的数据库下方。

要通过 Atlas 用户界面查看集群中的数据库和集合:

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。

  2. 如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。

  3. 在侧边栏中,单击 Database 标题下的 Data Explorer。

    The Data Explorer displays.

重要提示:您还可以单击集群的名称以打开 Cluster侧边栏,然后单击Data Explorer Shortcuts标题下的 。

2

单击数据库名称。

注意

Atlas 在此标签页上显示的文档计数是基于缓存的元数据并使用 collStats 得出的。此计数可能与集合中的实际文档数不同。例如,意外关机 可能会引发计数失败。请使用 db.collection.countDocuments()方法获得最准确的文档计数。

启动 MongoDB Charts 以可视化数据库和集合中的数据。

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。

  2. 如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。

  3. 在侧边栏中,单击 Database 标题下的 Data Explorer。

    The Data Explorer displays.

重要提示:您还可以单击集群的名称以打开 Cluster侧边栏,然后单击Data Explorer Shortcuts标题下的 。

2

To visualize data in MongoDB Charts from the Atlas UI, click Visualize Your Data when viewing a specific database or collection. Charts loads the data source and you can start building a chart in the Charts view. For detailed steps, see Build Charts.

要通过 Atlas UI 删除集合(包括其文档和索引):

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。

  2. 如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。

  3. 在侧边栏中,单击 Database 标题下的 Data Explorer。

    The Data Explorer displays.

重要提示:您还可以单击集群的名称以打开 Cluster侧边栏,然后单击Data Explorer Shortcuts标题下的 。

2

在 Connections 侧边栏中,单击要删除的集合旁边的 图标。在出现的下拉菜单中,单击 Drop Collection。

3

键入集合名称进行确认,然后单击 Drop Database(删除)。

If you have large data sets and perform high throughput operations, you can shard a collection to distribute data across the shards.

You cannot shard a collection through the Atlas UI. To shard a collection, first confirm that your cluster is a sharded cluster, then take the following steps to shard the collection in mongosh:

1

请参阅通过mongosh连接到集群。

2

若要在分片键上创建索引,运行以下命令:

db.<collection-name>.createIndex({<shard_key_definition>})

例子

为 sample_analytics.customers 集合的分片键创建索引:

db.sample_analytics.createIndex( { "username": 1 } )

要了解详情,请参阅:

3

要对集合进行分片,请运行以下命令:

sh.shardCollection("<database>.<collection>", { "<indexed-field>" : 1 } )

例子

要对 sample_analytics.customers 集合进行分片,请执行以下操作:

sh.shardCollection("sample_analytics.customers", { "username" : 1 } )

WARNING: If you shard a collection that already has a MongoDB Search index, you might experience a brief period of incomplete search results while the index is building on a shard. Also, if you add a shard for an already sharded collection that contains a MongoDB Search index, your search queries against that collection might return incomplete results until the initial sync process completes on the added shards. To learn more, see initial sync process.

To learn more, see Shard a Collection in the MongoDB manual.