在 Azure Cosmos DB for MongoDB 中创建集合

适用对象: MongoDB

本文介绍在 Azure Cosmos DB for MongoDB 中创建集合的不同方式。 它演示了如何使用 Azure 门户、Azure CLI、PowerShell 或受支持的 SDK 来创建集合。 本文演示如何创建集合、指定分区键和预配吞吐量。

注意

容器集合类似于关系数据库中的表。 在 Azure Cosmos DB for NoSQL 以及整个 Azure 门户中,我们使用“容器”;而在 Azure Cosmos DB for MongoDB 的上下文中,我们使用“集合”,以便与 Mongo DB 中使用的术语匹配。

本文介绍在 Azure Cosmos DB for MongoDB 中创建集合的不同方式。 如果使用的是其他 API,请参阅 API for NoSQLAPI for CassandraAPI for Gremlin表 API 这几篇文章来创建集合。

注意

创建集合时,请确保不创建名称相同但大小写不同的两个集合。 这是因为 Azure 平台的某些部分不区分大小写,这可能会对具有此类名称的集合导致遥测和操作混乱/冲突。

使用 Azure 门户进行创建

  1. 登录到 Azure 门户

  2. 创建新的 Azure Cosmos DB 帐户或选择现有的帐户。

  3. 打开“数据资源管理器”窗格,然后选择“新建容器” 。 接下来,请提供以下详细信息:

    • 表明要创建新数据库还是使用现有数据库。
    • 输入容器 ID。
    • 输入分片键。
    • 输入要进行预配的吞吐量(例如,1000 RU)。
    • 选择“确定”。

    Screenshot of Azure Cosmos DB for MongoDB, Add Container dialog box

使用 .NET SDK 创建

var bson = new BsonDocument
{
    { "customAction", "CreateCollection" },
    { "collection", "<CollectionName>" },//update CollectionName
    { "shardKey", "<ShardKeyName>" }, //update ShardKey
    { "offerThroughput", 400} //update Throughput
};
var shellCommand = new BsonDocumentCommand<BsonDocument>(bson);
// Create a collection with a partition key by using Mongo Driver:
db.RunCommand(shellCommand);

如果创建集合时遇到超时异常,请执行读取操作来验证是否已成功创建集合。 成功完成集合创建操作之前,读取操作将引发异常。 有关创建操作所支持的状态代码列表,请参阅 Azure Cosmos DB 的 HTTP 状态代码一文。

使用 Azure CLI 创建

使用 Azure CLI 为 Azure Cosmos DB for API for MongoDB 创建集合。 有关所有 Azure Cosmos DB API 的所有 Azure CLI 示例的列表,请参阅 Azure Cosmos DB 的 Azure CLI 示例

使用 PowerShell 创建

使用 PowerShell 为 Azure Cosmos DB for API for MongoDB 创建集合。 有关所有 Azure Cosmos DB API 的所有 PowerShell 示例的列表,请参阅 PowerShell 示例

使用 Azure 资源管理器模板来创建集合

使用资源管理器模板为 Azure Cosmos DB for API for MongoDB 创建集合

后续步骤