Rediger

Del via


Create a collection in Azure Cosmos DB for MongoDB

APPLIES TO: MongoDB

This article explains the different ways to create a collection in Azure Cosmos DB for MongoDB. It shows how to create a collection using Azure portal, Azure CLI, PowerShell, or supported SDKs. This article demonstrates how to create a collection, specify the partition key, and provision throughput.

Note

Containers and collections are similar to a table in a relational database. We refer to containers in the Azure Cosmos DB for NoSQL and throughout the Azure portal, while we use collections in the context of the Azure Cosmos DB for MongoDB to match the terminology used in MongoDB.

This article explains the different ways to create a collection in Azure Cosmos DB for MongoDB. If you are using a different API, see API for NoSQL, API for Cassandra, API for Gremlin, and API for Table articles to create the collection.

Note

When creating collections, make sure you don’t create two collections with the same name but different casing. 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 collections with such names.

Create using Azure portal

  1. Sign in to the Azure portal.

  2. Create a new Azure Cosmos DB 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.
    • Enter a container ID.
    • Enter a shard key.
    • Enter a throughput to be provisioned (for example, 1000 RUs).
    • Select OK.

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

Create using .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);

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. For the list of status codes supported by the create operation see the HTTP Status Codes for Azure Cosmos DB article.

Create using Azure CLI

Create a collection for Azure Cosmos DB for API for MongoDB with Azure CLI. For a listing of all Azure CLI samples across all Azure Cosmos DB APIs see, Azure CLI samples for Azure Cosmos DB.

Create using PowerShell

Create a collection for Azure Cosmos DB for API for MongoDB with PowerShell. For a listing of all PowerShell samples across all Azure Cosmos DB APIs see, PowerShell Samples

Create a collection using Azure Resource Manager templates

Create a collection for Azure Cosmos DB for API for MongoDB with Resource Manager template.

Next steps