Create a database and collection with autoscale for Azure Cosmos DB - API for MongoDB
Article 08/22/2024
4 contributors
Feedback
In this article
Sample script
Clean up deployment
Script explanation
Next steps
APPLIES TO:
MongoDB
This sample requires Azure PowerShell Az 5.4.0 or later. Run Get-Module -ListAvailable Az
to see which versions are installed.
If you need to install, see Install Azure PowerShell module .
Run Connect-AzAccount to sign in to Azure.
# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos MongoDB API account with automatic failover,
# a database, and a collection with autoscale throughput.
# --------------------------------------------------
Function New-RandomString{Param ([Int]$Length = 10) return $(-join ((97..122) + (48..57) | Get-Random -Count $Length | ForEach-Object {[char]$_}))}
# --------------------------------------------------
$uniqueId = New-RandomString -Length 7 # Random alphanumeric string for unique resource names
$apiKind = "MongoDB"
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East Us" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West Us" -FailoverPriority 1 -IsZoneRedundant 0
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "cosmos-$uniqueId" # Must be all lower case
$serverVersion = "4.2" #3.2, 3.6, 4.0 or 4.2
$consistencyLevel = "Session"
$databaseName = "myDatabase"
$collectionName = "myCollection"
$autoscaleMaxThroughput = 1000 #minimum = 1000
$shardKey = "user_id"
$partitionKeys = @("user_id", "user_address")
$ttlKeys = @("_ts")
$ttlInSeconds = 604800
# --------------------------------------------------
Write-Host "Creating account $accountName"
$account = New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
-LocationObject $locations -Name $accountName -ApiKind $apiKind `
-DefaultConsistencyLevel $consistencyLevel `
-EnableAutomaticFailover:$true -ServerVersion $serverVersion
Write-Host "Creating database $databaseName"
$database = New-AzCosmosDBMongoDBDatabase -ParentObject $account `
-Name $databaseName
$index1 = New-AzCosmosDBMongoDBIndex -Key $partitionKeys -Unique $true
$index2 = New-AzCosmosDBMongoDBIndex -Key $ttlKeys -TtlInSeconds $ttlInSeconds
$indexes = @($index1, $index2)
Write-Host "Creating collection $collectionName"
$collection = New-AzCosmosDBMongoDBCollection -ParentObject $database `
-Name $collectionName -AutoscaleMaxThroughput $autoscaleMaxThroughput `
-Shard $shardKey -Index $indexes
After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.
Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"
This script uses the following commands. Each command in the table links to command specific documentation.
Expand table
For more information on the Azure PowerShell, see Azure PowerShell documentation .