Create an Azure Cosmos container

This article explains the different ways to create a container (collection, table, or graph). You use the Azure portal, the Azure CLI, or supported SDKs. This article demonstrates how to create a container, specify the partition key, and provision throughput.

Create a container by using Azure portal

SQL API

  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 Collection. Next, provide the following details:

    • Indicate whether you are creating a new database or using an existing one.
    • Enter a collection ID.
    • Enter a partition key.
    • Enter a throughput (for example, 1000 RUs).
    • Select OK.

Screenshot of Data Explorer pane, with New Collection highlighted

Azure Cosmos DB API for MongoDB

  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 Collection. Next, provide the following details:

    • Indicate whether you are creating a new database or using an existing one.
    • Enter a collection ID.
    • Select Unlimited storage capacity.
    • Enter a shard key.
    • Enter a throughput (for example, 1000 RUs).
    • Select OK.

Screenshot of Azure Cosmos DB API for MongoDB, Add Collection dialog box

Cassandra API

  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 Table. Next, provide the following details:

    • Indicate whether you are creating a new keyspace, or using an existing one.
    • Enter a table name.
    • Enter the properties and specify a primary key.
    • Enter a throughput (for example, 1000 RUs).
    • Select OK.

Screenshot of Cassandra API, Add Table dialog box

Note

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

Gremlin API

  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 Graph. Next, provide the following details:

    • Indicate whether you are creating a new database, or using 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.

Screenshot of Gremlin API, Add Graph dialog box

Table API

  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 Table. Next, provide the following details:

    • Enter a table ID.
    • Select Unlimited storage capacity.
    • Enter a throughput (for example, 1000 RUs).
    • Select OK.

Screenshot of Table API, Add Table dialog box

Note

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

Create a container by 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 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 by 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 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 request units. To create a new collection with throughput, use the Azure portal or SQL API.

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