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.
Sign in to the Azure portal.
Create a new Azure Cosmos DB account, or select an existing account.
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.
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 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 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 for Azure Cosmos DB for API for MongoDB with Resource Manager template.
- Partitioning in Azure Cosmos DB
- Request Units in Azure Cosmos DB
- Provision throughput on containers and databases
- Work with Azure Cosmos DB account
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vcores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner