MongoDB database

Learn how to use a Mongo database in your application

Overview

The mongodb.com/MongoDatabase resource represents a Mongo database.

Resource format


resource db 'Applications.Datastores/mongoDatabases@2023-10-01-preview' = {
  name: 'db'
  properties: {
    environment: environment
    application: app.id
    recipe: {
      // Name a specific Recipe to use
      name: 'azure-cosmosdb'
      // Set optional/required parameters (specific to the Recipe)
      parameters: {
        size: 'large'
      }
    }
  }
}

resource db 'Applications.Datastores/mongoDatabases@2023-10-01-preview' = {
  name: 'db'
  properties: {
    environment: environment
    application: app.id
    resourceProvisioning: 'manual'
    host: substring(cosmosAccount.properties.documentEndpoint, 0, lastIndexOf(cosmosAccount.properties.documentEndpoint, ':'))
    port: int(split(substring(cosmosAccount.properties.documentEndpoint,lastIndexOf(cosmosAccount.properties.documentEndpoint, ':') + 1), '/')[0])
    database: cosmosAccount::database.name
    username: ''
    resources: [
      { id: cosmosAccount.id }
    ]
    secrets: {
      connectionString: cosmosAccount.listConnectionStrings().connectionStrings[0].connectionString
      password: base64ToString(cosmosAccount.listKeys().primaryMasterKey)
    }
  }
}

Top-level

KeyRequiredDescriptionExample
nameyThe name of your resource.mongo
locationyThe location of your resource. See common values for more information.global
propertiesyProperties of the resource.See below

Properties

PropertyRequiredDescriptionExample(s)
applicationnThe ID of the application resource this resource belongs to.app.id
environmentyThe ID of the environment resource this resource belongs to.env.id
resourceProvisioningnSpecifies how the underlying service/resource is provisioned and managed. Options are to provision automatically via ‘recipe’ or provision manually via ‘manual’. Selection determines which set of fields to additionally require. Defaults to ‘recipe’.manual
recipenConfiguration for the Recipe which will deploy the backing infrastructure.See below
resourcesnAn array of resources which underlay this resource. For example, an Azure CosmosDB database ID if the MongoDB resource is leveraging CosmosDB.See below
databasenDatabase name of the target MongoDBmongodb-prod
hostnThe MongoDB host name.mongodb0.example.com
portnThe MongoDB port.4242
usernamenThe username for the MongoDB.'myusername'
secretsnSecrets used when building the resource from values.See below

Recipe

PropertyRequiredDescriptionExample(s)
namenSpecifies the name of the Recipe that should be deployed. If not set, the name defaults to default.name: 'azure-prod'
parametersnAn object that contains a list of parameters to set on the Recipe.{ size: 'large' }

Resources

PropertyRequiredDescriptionExample(s)
idnResource ID of the supporting resource.account.id

Secrets

PropertyRequiredDescriptionExample(s)
connectionStringnThe connection string for the MongoDb. Write only.mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:4242/?authSource=admin
passwordnThe password for the MongoDB. Write only.mypassword

Methods

The following methods are available on the Mongo database resource:

MethodDescriptionExample
listSecrets()Get the secrets for the MongoDb.listSecrets().connectionString

Resource provisioning

Provision with a Recipe

Recipes automate infrastructure provisioning using approved templates. You can specify a Recipe name that is registered in the environment or omit the name and use the “default” Recipe.

Parameters can also optionally be specified for the Recipe.

Provision manually

If you want to manually manage your infrastructure provisioning outside of Recipes, you can set resourceProvisioning to 'manual' and provide all necessary parameters and values in order for Radius to be able to deploy/connect to the desired infrastructure.

Environment variables for connections

Other Radius resources, such as containers, may connect to a MongoDB resource via connections. When a connection to MongoDB named, for example, myconnection is declared, Radius injects values into environment variables that are then used to access the connected MongoDB resource:

Environment variableExample(s)
CONNECTION_MYCONNECTION_HOSTmongodb0.example.com
CONNECTION_MYCONNECTION_PORT4242
CONNECTION_MYCONNECTION_DATABASEmongodb-prod
CONNECTION_MYCONNECTION_USERNAMEmyusername
CONNECTION_MYCONNECTION_PASSWORDmypassword
CONNECTION_MYCONNECTION_CONNECTIONSTRINGmongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:4242/?authSource=admin