Manage Azure Cosmos DB for MongoDB resources using Bicep
APPLIES TO: MongoDB
In this article, you learn how to use Bicep to deploy and manage your Azure Cosmos DB accounts for API for MongoDB, databases, and collections.
This article shows Bicep samples for API for MongoDB accounts. You can also find Bicep samples for SQL, Cassandra, Gremlin, and Table APIs.
Important
- Account names are limited to 44 characters, all lowercase.
- To change the throughput values, redeploy the template with updated RU/s.
- When you add or remove locations to an Azure Cosmos DB account, you can't simultaneously modify other properties. These operations must be done separately.
To create any of the Azure Cosmos DB resources below, copy the following example into a new bicep file. You can optionally create a parameters file to use when deploying multiple instances of the same resource with different names and values. There are many ways to deploy Azure Resource Manager templates including, Azure CLI and Azure PowerShell.
API for MongoDB with autoscale provisioned throughput
This template will create an Azure Cosmos DB account for API for MongoDB (3.2, 3.6, 4.0, or 4.2) with two collections that share autoscale throughput at the database level.
@description('Cosmos DB account name')
param accountName string = 'mongodb-${uniqueString(resourceGroup().id)}'
@description('Location for the Cosmos DB account.')
param location string = resourceGroup().location
@description('The primary replica region for the Cosmos DB account.')
param primaryRegion string
@description('The secondary replica region for the Cosmos DB account.')
param secondaryRegion string
@description('Specifies the MongoDB server version to use.')
@allowed([
'3.2'
'3.6'
'4.0'
])
param serverVersion string = '4.0'
@description('The default consistency level of the Cosmos DB account.')
@allowed([
'Eventual'
'ConsistentPrefix'
'Session'
'BoundedStaleness'
'Strong'
])
param defaultConsistencyLevel string = 'Session'
@description('Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 1000000. Multi Region: 100000 to 1000000.')
@minValue(10)
@maxValue(2147483647)
param maxStalenessPrefix int = 100000
@description('Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400.')
@minValue(5)
@maxValue(86400)
param maxIntervalInSeconds int = 300
@description('The name for the Mongo DB database')
param databaseName string
@description('The name for the first Mongo DB collection')
param collection1Name string
@description('The name for the second Mongo DB collection')
param collection2Name string
@description('Maximum throughput when using Autoscale Throughput Policy for the Database')
@minValue(4000)
@maxValue(1000000)
param autoscaleMaxThroughput int = 4000
var accountName_var = toLower(accountName)
var consistencyPolicy = {
Eventual: {
defaultConsistencyLevel: 'Eventual'
}
ConsistentPrefix: {
defaultConsistencyLevel: 'ConsistentPrefix'
}
Session: {
defaultConsistencyLevel: 'Session'
}
BoundedStaleness: {
defaultConsistencyLevel: 'BoundedStaleness'
maxStalenessPrefix: maxStalenessPrefix
maxIntervalInSeconds: maxIntervalInSeconds
}
Strong: {
defaultConsistencyLevel: 'Strong'
}
}
var locations = [
{
locationName: primaryRegion
failoverPriority: 0
isZoneRedundant: false
}
{
locationName: secondaryRegion
failoverPriority: 1
isZoneRedundant: false
}
]
resource accountName_resource 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = {
name: accountName_var
location: location
kind: 'MongoDB'
properties: {
consistencyPolicy: consistencyPolicy[defaultConsistencyLevel]
locations: locations
databaseAccountOfferType: 'Standard'
apiProperties: {
serverVersion: serverVersion
}
}
}
resource accountName_databaseName 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases@2021-04-15' = {
parent: accountName_resource
name: databaseName
properties: {
resource: {
id: databaseName
}
options: {
autoscaleSettings: {
maxThroughput: autoscaleMaxThroughput
}
}
}
}
resource accountName_databaseName_collection1Name 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2021-04-15' = {
parent: accountName_databaseName
name: collection1Name
properties: {
resource: {
id: collection1Name
shardKey: {
user_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'user_id'
'user_address'
]
}
options: {
unique: true
}
}
{
key: {
keys: [
'_ts'
]
options: {
expireAfterSeconds: 2629746
}
}
}
]
options: {
'If-Match': '<ETag>'
}
}
}
}
resource accountName_databaseName_collection2Name 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2021-04-15' = {
parent: accountName_databaseName
name: collection2Name
properties: {
resource: {
id: collection2Name
shardKey: {
company_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'company_id'
'company_address'
]
}
options: {
unique: true
}
}
{
key: {
keys: [
'_ts'
]
options: {
expireAfterSeconds: 2629746
}
}
}
]
options: {
'If-Match': '<ETag>'
}
}
}
}
API for MongoDB with standard provisioned throughput
Create an Azure Cosmos DB account for API for MongoDB (3.2, 3.6, 4.0, or 4.2) with two collections that share 400 RU/s standard (manual) throughput at the database level.
@description('Cosmos DB account name')
param accountName string = 'mongodb-${uniqueString(resourceGroup().id)}'
@description('Location for the Cosmos DB account.')
param location string = resourceGroup().location
@description('The primary replica region for the Cosmos DB account.')
param primaryRegion string
@description('The secondary replica region for the Cosmos DB account.')
param secondaryRegion string
@description('The default consistency level of the Cosmos DB account.')
@allowed([
'Eventual'
'ConsistentPrefix'
'Session'
'BoundedStaleness'
'Strong'
])
param defaultConsistencyLevel string = 'Session'
@description('Specifies the MongoDB server version to use.')
@allowed([
'3.2'
'3.6'
'4.0'
])
param serverVersion string = '4.0'
@description('Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 1000000. Multi Region: 100000 to 1000000.')
@minValue(10)
@maxValue(2147483647)
param maxStalenessPrefix int = 100000
@description('Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400.')
@minValue(5)
@maxValue(86400)
param maxIntervalInSeconds int = 300
@description('The name for the Mongo DB database')
param databaseName string
@description('The shared throughput for the Mongo DB database')
@minValue(400)
@maxValue(1000000)
param throughput int = 400
@description('The name for the first Mongo DB collection')
param collection1Name string
@description('The name for the second Mongo DB collection')
param collection2Name string
var accountName_var = toLower(accountName)
var consistencyPolicy = {
Eventual: {
defaultConsistencyLevel: 'Eventual'
}
ConsistentPrefix: {
defaultConsistencyLevel: 'ConsistentPrefix'
}
Session: {
defaultConsistencyLevel: 'Session'
}
BoundedStaleness: {
defaultConsistencyLevel: 'BoundedStaleness'
maxStalenessPrefix: maxStalenessPrefix
maxIntervalInSeconds: maxIntervalInSeconds
}
Strong: {
defaultConsistencyLevel: 'Strong'
}
}
var locations = [
{
locationName: primaryRegion
failoverPriority: 0
isZoneRedundant: false
}
{
locationName: secondaryRegion
failoverPriority: 1
isZoneRedundant: false
}
]
resource accountName_resource 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = {
name: accountName_var
location: location
kind: 'MongoDB'
properties: {
consistencyPolicy: consistencyPolicy[defaultConsistencyLevel]
locations: locations
databaseAccountOfferType: 'Standard'
apiProperties: {
serverVersion: serverVersion
}
}
}
resource accountName_databaseName 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases@2021-04-15' = {
name: '${accountName_resource.name}/${databaseName}'
properties: {
resource: {
id: databaseName
}
options: {
throughput: throughput
}
}
}
resource accountName_databaseName_collection1Name 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2021-04-15' = {
name: '${accountName_databaseName.name}/${collection1Name}'
properties: {
resource: {
id: collection1Name
shardKey: {
user_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'user_id'
'user_address'
]
}
options: {
unique: true
}
}
{
key: {
keys: [
'_ts'
]
}
options: {
expireAfterSeconds: 2629746
}
}
]
options: {
'If-Match': '<ETag>'
}
}
}
}
resource accountName_databaseName_collection2Name 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2021-04-15' = {
name: '${accountName_databaseName.name}/${collection2Name}'
properties: {
resource: {
id: collection2Name
shardKey: {
company_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'company_id'
'company_address'
]
}
options: {
unique: true
}
}
{
key: {
keys: [
'_ts'
]
}
options: {
expireAfterSeconds: 2629746
}
}
]
options: {
'If-Match': '<ETag>'
}
}
}
}
Next steps
Here are some additional resources:
- Bicep documentation
- Install Bicep tools
- 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