在 Azure Cosmos DB API for MongoDB 中创建容器Create a container in Azure Cosmos DB API for MongoDB

适用于: Azure Cosmos DB API for MongoDB

本文介绍在 Azure Cosmos DB API for MongoDB 中创建容器的不同方式。This article explains the different ways to create a container in Azure Cosmos DB API for MongoDB. 它演示了如何使用 Azure 门户、Azure CLI、PowerShell 或受支持的 SDK 来创建容器。It shows how to create a container using Azure portal, Azure CLI, PowerShell, or supported SDKs. 本文演示如何创建容器、指定分区键和预配吞吐量。This article demonstrates how to create a container, specify the partition key, and provision throughput.

本文介绍在 Azure Cosmos DB API for MongoDB 中创建容器的不同方式。This article explains the different ways to create a container in Azure Cosmos DB API for MongoDB. 如果你使用其他 API,请参阅 SQL APICassandra APIGremlin API表 API 这几篇文章来创建容器。If you are using a different API, see SQL API, Cassandra API, Gremlin API, and Table API articles to create the container.

备注

创建容器时,请确保不创建名称相同但大小写不同的两个容器。When creating containers, make sure you don't create two containers with the same name but different casing. 这是因为 Azure 平台的某些部分不区分大小写,这可能会对具有此类名称的容器导致遥测和操作混乱/冲突。That's because some parts of the Azure platform are not case-sensitive, and this can result in confusion/collision of telemetry and actions on containers with such names.

使用 Azure 门户进行创建Create using Azure portal

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有的帐户。Create a new Azure Cosmos account, or select an existing account.

  3. 打开“数据资源管理器”窗格,然后选择“新建容器” 。Open the Data Explorer pane, and select New Container. 接下来,请提供以下详细信息:Next, provide the following details:

    • 表明要创建新数据库还是使用现有数据库。Indicate whether you are creating a new database or using an existing one.
    • 输入容器 ID。Enter a container ID.
    • 输入分片键。Enter a shard key.
    • 输入要进行预配的吞吐量(例如,1000 RU)。Enter a throughput to be provisioned (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    Azure Cosmos DB API for MongoDB“添加容器”对话框的屏幕截图

使用 .NET SDK 创建Create using .NET SDK

// Create a collection with a partition key by using Mongo Shell:
db.runCommand( { shardCollection: "myDatabase.myCollection", key: { myShardKey: "hashed" } } )

备注

MongoDB 网络协议不能理解请求单位的概念。MongoDB wire protocol does not understand the concept of Request Units. 要创建一个新集合,并在其上提供吞吐量,请使用 Azure 门户或用于 SQL API 的 Cosmos DB SDK。To create a new collection with provisioned throughput on it, use the Azure portal or Cosmos DB SDKs for SQL API.

如果创建集合时遇到超时异常,请执行读取操作来验证是否已成功创建集合。If you encounter timeout exception when creating a collection, do a read operation to validate if the collection was created successfully. 成功完成集合创建操作之前,读取操作将引发异常。The read operation throws an exception until the collection create operation is successful. 有关创建操作所支持的状态代码列表,请参阅 Azure Cosmos DB 的 HTTP 状态代码一文。For the list of status codes supported by the create operation see the HTTP Status Codes for Azure Cosmos DB article.

使用 Azure CLI 创建Create using Azure CLI

使用 Azure CLI 为 Azure Cosmos DB for MongoDB API 创建集合Create a collection for Azure Cosmos DB for MongoDB API with Azure CLI. 有关所有 Azure Cosmos DB API 的所有 Azure CLI 示例的列表,请参阅 Azure Cosmos DB 的 Azure CLI 示例For a listing of all Azure CLI samples across all Azure Cosmos DB APIs see, Azure CLI samples for Azure Cosmos DB.

使用 PowerShell 创建Create using PowerShell

使用 PowerShell 为 Azure Cosmos DB for MongoDB API 创建集合Create a collection for Azure Cosmos DB for MongoDB API with PowerShell. 有关所有 Azure Cosmos DB API 的所有 PowerShell 示例的列表,请参阅 PowerShell 示例For a listing of all PowerShell samples across all Azure Cosmos DB APIs see, PowerShell Samples

使用 Azure 资源管理器模板来创建容器Create a container using Azure Resource Manager templates

使用资源管理器模板为 Azure Cosmos DB for MongoDB API 创建集合Create a collection for Azure Cosmos DB for MongoDB API with Resource Manager template.

后续步骤Next steps