Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
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.
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now