Create a container in Azure Cosmos DB

This article explains the different ways to create a container (collection, table, graph). A container can be created by using the Azure portal, Azure CLI, or supported SDKs. This article demonstrates how to create a container, specify the partition key and provision throughput.

Create a container using Azure portal

SQL API

  1. Sign in to Azure portal.

  2. Create a new Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane and select New Collection. Next fill the form with the following details:

    • Create a new database or use an existing one.
    • Enter a Collection Id.
    • Enter a Partition key.
    • Enter a throughput, for example 1000 RUs.
    • Select OK.

SQL API creates a collection

Azure Cosmos DB's API for MongoDB

  1. Sign in to Azure portal.

  2. Create a new Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane and select New Collection. Next fill the form with the following details:

    • Create a new database or use an existing one.
    • Enter a Collection Id.
    • Select Unlimited storage capacity.
    • Enter a Shard key.
    • Enter a throughput, for example 1000 RUs.
    • Select OK.

Azure Cosmos DB's API for MongoDB creates a collection

Cassandra API

  1. Sign in to Azure portal.

  2. Create a new Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane and select New Table. Next fill the form with the following details:

    • Create a new Keyspace or use an existing one.
    • Enter a table name.
    • Enter the properties and specify a PRIMARY KEY.
    • Enter a throughput, for example 1000 RUs.
    • Select OK.

Cassandra API creates a collection

Note

For Cassandra API, the primary key is used as the partition key.

Gremlin API

  1. Sign in to Azure portal.

  2. Create a new Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane and select New Graph. Next fill the form with the following details:

    • Create a new database or use an existing one.
    • Enter a Graph id.
    • Select Unlimited storage capacity.
    • Enter a Partition key for vertices.
    • Enter a throughput, for example 1000 RUs.
    • Select OK.

Gremlin API creates a collection

Table API

  1. Sign in to Azure portal.

  2. Create a new Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane and select New Table. Next fill the form with the following details:

    • Enter a Table Id.
    • Select Unlimited storage capacity.
    • Enter a throughput, for example 1000 RUs.
    • Select OK.

Table API creates a collection

Note

For Table API, the partition key is specified each time you add a new row.

Create a container using Azure CLI

SQL API

# Create a container with a partition key and provision 1000 RU/s throughput.

az cosmosdb collection create \
    --resource-group $resourceGroupName \
    --collection-name $containerName \
    --name $accountName \
    --db-name $databaseName \
    --partition-key-path /myPartitionKey \
    --throughput 1000

Azure Cosmos DB's API for MongoDB

# Create a collection with a shard key and provision 1000 RU/s throughput.
az cosmosdb collection create \
    --resource-group $resourceGroupName \
    --collection-name $collectionName \
    --name $accountName \
    --db-name $databaseName \
    --partition-key-path /myShardKey \
    --throughput 1000

Cassandra API

# Create a table with a partition/primary key and provision 1000 RU/s throughput.
az cosmosdb collection create \
    --resource-group $resourceGroupName \
    --collection-name $tableName \
    --name $accountName \
    --db-name $keyspaceName \
    --partition-key-path /myPrimaryKey \
    --throughput 1000

Gremlin API

# Create a graph with a partition key and provision 1000 RU/s throughput.
az cosmosdb collection create \
    --resource-group $resourceGroupName \
    --collection-name $graphName \
    --name $accountName \
    --db-name $databaseName \
    --partition-key-path /myPartitionKey \
    --throughput 1000

Table API

# Create a table with 1000 RU/s
# Note: you don't need to specify partition key in the following command because the partition key is set on each row.
az cosmosdb collection create \
    --resource-group $resourceGroupName \
    --collection-name $tableName \
    --name $accountName \
    --db-name $databaseName \
    --throughput 1000

Create a container using .NET SDK

SQL API and Gremlin API

// Create a container with a partition key and provision 1000 RU/s throughput.
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "myContainerName";
myCollection.PartitionKey.Paths.Add("/myPartitionKey");

await client.CreateDocumentCollectionAsync(
    UriFactory.CreateDatabaseUri("myDatabaseName"),
    myCollection,
    new RequestOptions { OfferThroughput = 1000 });

Azure Cosmos DB's API for MongoDB

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

Note

MongoDB wire protocol does not have a concept of request units. To create a new collection with throughput, use the Azure Portal or SQL API as shown in theprevious examples.

Cassandra API

// Create a Cassandra table with a partition/primary key and provision 1000 RU/s throughput.
session.Execute(CREATE TABLE myKeySpace.myTable(
    user_id int PRIMARY KEY,
    firstName text,
    lastName text) WITH cosmosdb_provisioned_throughput=1000);

Next steps

See the following articles to learn about partitioning in Cosmos DB: