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

Key Required Description Example
name y The name of your resource. mongo
location y The location of your resource. See common values for more information. global
properties y Properties of the resource. See below

Properties

Property Required Description Example(s)
application n The ID of the application resource this resource belongs to. app.id
environment y The ID of the environment resource this resource belongs to. env.id
resourceProvisioning n Specifies 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
recipe n Configuration for the Recipe which will deploy the backing infrastructure. See below
resources n An array of resources which underlay this resource. For example, an Azure CosmosDB database ID if the MongoDB resource is leveraging CosmosDB. See below
database n Database name of the target MongoDB mongodb-prod
host n The MongoDB host name. mongodb0.example.com
port n The MongoDB port. 4242
username n The username for the MongoDB. 'myusername'
secrets n Secrets used when building the resource from values. See below

Recipe

Property Required Description Example(s)
name n Specifies the name of the Recipe that should be deployed. If not set, the name defaults to default. name: 'azure-prod'
parameters n An object that contains a list of parameters to set on the Recipe. { size: 'large' }

Resources

Property Required Description Example(s)
id n Resource ID of the supporting resource. account.id

Secrets

Property Required Description Example(s)
connectionString n The connection string for the MongoDb. Write only. mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:4242/?authSource=admin
password n The password for the MongoDB. Write only. mypassword

Methods

The following methods are available on the Mongo database resource:

Method Description
connectionString() Get the connection string for the MongoDb.
username() Get the username for the MongoDB.
password() Get the password for the MongoDB.

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 variable Example(s)
CONNECTION_MYCONNECTION_HOST mongodb0.example.com
CONNECTION_MYCONNECTION_PORT 4242
CONNECTION_MYCONNECTION_DATABASE mongodb-prod
CONNECTION_MYCONNECTION_USERNAME myusername
CONNECTION_MYCONNECTION_PASSWORD mypassword
CONNECTION_MYCONNECTION_CONNECTIONSTRING mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:4242/?authSource=admin