How to Create a Cosmos DB Database with MongoDB API Using Azure CLI

Azure Cosmos DB supports creation of databases which are compatible with MongoDB API. You can use standard MongoDB query language to access and modify data in such a database. However, please note that there are limitations in MongoDB API support and for aggregations, you will need to explicitly turn on aggregation support when creating the Cosmos DB account.

The following shell script can be used to create a MongoDB API compatible Cosmos DB database using Azure CLI. Follow these instructions if you don't have Azure CLI installed on your machine. Also note that the following script will run only on linux or mac systems.

The cosmos db shell script given below asks for the following parameters,

  • Subscription id: This is subscription id under which you want all resources to be created. This value is available from the Azure portal under the "Cost management + Billing" section.
  • Resource group name: In Azure, all resources are organized under a container called resource group. Give a name under which you want your cosmos db account to be organized.
  • Cosmos db account name: This is the name you want to give to your cosmos db account. Note that you can have multiple databases under a single account. Also note that the name must be a valid domain name prefix and should be globally unique in Azure. You will be accessing your account as [cosmos_account_name]
  • Cosmos db database name: This is the name you want to give your MongDB instance. This need not be globally unique.

Note that the location for the resource group is hard coded as "US West" region. However, you can easily change it to an input parameter. Resources created under the group will be automatically assigned to the same region.

In Cosmos DB, you can specify the scalability factor of request units (database pricing depends on this value) at the database level or at the collection level. The following script sets the request unit limit to 400 at the database level to minimize cost. This is ok for test systems, but in production you may need a higher limit or set the limits at the collection level.

Note the use of "--capabilities" flag to turn on the aggregations preview feature. I have also set "--default-consistency-level" set to "Strong" to enable consistent reads/writes at the cost of performance.

Once the database is created, you can view the database from the Azure portal. You can also access the DB credentials from the portal. Here is the Azure CLI script for creating MongoDB compatible database in Cosmos DB,


declare resource_location="westus"
echo "Creating cosmos db in $resource_location"

if [[ -z "$subscription_id" ]]; then
  echo "Please enter subscription id:"
  read subscription_id
  [[ "${subscription_id:?}" ]]

if [[ -z "$resource_group_name" ]]; then
  echo "Please enter resource group name for cosmos db:"
  read resource_group_name
  [[ "${resource_group_name:?}" ]]
if [[ -z "$cosmos_account_name" ]]; then
  echo "Please enter cosmos account name:"
  read cosmos_account_name
  [[ "${cosmos_account_name:?}" ]]
if [[ -z "$cosmos_db_name" ]]; then
  echo "Please enter cosmos database name:"
  read cosmos_db_name
  [[ "${cosmos_db_name:?}" ]]

az group create \
  --subscription $subscription_id \
  --name $resource_group_name \
  --location $resource_location

az cosmosdb create \
  --subscription $subscription_id \
  --resource-group $resource_group_name \
  --name $cosmos_account_name \
  --kind MongoDB \
  --capabilities EnableAggregationPipeline \
  --default-consistency-level "Strong" \

az cosmosdb database create \
  --subscription $subscription_id \
  --resource-group $resource_group_name \
  --name $cosmos_account_name \
  --db-name $cosmos_db_name \
  --throughput 400