You can manage the collections in your clusters using the Atlas UI and mongosh.
所需角色
下表描述了管理 Atlas 项目中集合所需的角色:
操作 | 所需角色 |
|---|---|
创建集合 | |
查看集合 | 至少是 |
删除集合 | |
碎片集合 | 以下角色之一: |
创建集合
提示
要在新数据库中创建第一个集合,请参阅创建数据库。
重要
您无法在 config 和 system 数据库上创建新集合。在不久的将来,Atlas 将不再支持向这些数据库上的现有集合写入数据。
要通过 Atlas 用户界面在现有数据库中创建集合:
在Atlas中,转到项目的 Data Explorer 页面
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
The Data Explorer displays.
输入 Collection Name(密钥保管库凭证)。
在 Create Collection 对话框中,输入要创建的集合的名称。
Atlas还提供 Additional preferences。您可以从以下选项中进行选择:
重要
请勿在集合名称中包含敏感信息。
For more information on MongoDB collection names, see Naming Restrictions.
可选。指定时间序列集合。
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.
查看集合
要通过 Atlas 用户界面查看集群中的数据库和集合:
AtlasGoData Explorer在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
The Data Explorer displays.
重要提示:您还可以单击集群的名称以打开 Cluster侧边栏,然后单击Data Explorer Shortcuts标题下的 。
查看数据库中的集合。
单击数据库名称。
注意
Atlas 在此标签页上显示的文档计数是基于缓存的元数据并使用 collStats 得出的。此计数可能与集合中的实际文档数不同。例如,意外关机 可能会引发计数失败。请使用 db.collection.countDocuments()方法获得最准确的文档计数。
可视化集合数据
启动 MongoDB Charts 以可视化数据库和集合中的数据。
AtlasGoData Explorer在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
The Data Explorer displays.
重要提示:您还可以单击集群的名称以打开 Cluster侧边栏,然后单击Data Explorer Shortcuts标题下的 。
启动 MongoDB Charts。
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 删除集合(包括其文档和索引):
AtlasGoData Explorer在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
The Data Explorer displays.
重要提示:您还可以单击集群的名称以打开 Cluster侧边栏,然后单击Data Explorer Shortcuts标题下的 。
将集合分片
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:
Connect to MongoDB from mongosh.
对您想要分片的集合进行分片操作。
要对集合进行分片,请运行以下命令:
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.