Create an Azure Cosmos container

This article explains the different ways to create an Azure Cosmos container (collection, table, or graph). You can use Azure portal, Azure CLI, or supported SDKs for this. 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 the Azure portal.

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

    Screenshot of Data Explorer pane, with New Container highlighted

Azure Cosmos DB API for MongoDB

  1. Sign in to the Azure portal.

  2. 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.
    • 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 API for MongoDB, Add Container dialog box

Cassandra API

  1. Sign in to the Azure portal.

  2. Create a new Azure Cosmos 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 to be provisioned (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 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 to be provisioned (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 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.
    • Enter a throughput to be provisioned (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 using Azure CLI

The links below show how to create container resources for Azure Cosmos DB using Azure CLI.

For a listing of all Azure CLI samples across all Azure Cosmos DB APIs see, SQL API, Cassandra API, MongoDB API, Gremlin API, and Table API

Create a container using PowerShell

The links below show how to create container resources for Azure Cosmos DB using PowerShell.

For a listing of all Azure CLI samples across all Azure Cosmos DB APIs see, SQL API, Cassandra API, MongoDB API, Gremlin API, and Table API

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 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 understand the concept of Request Units. To create a new collection with provisioned throughput on it, use the Azure portal or Cosmos DB SDKs for 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