To activate MongoDB and then have it accessed by the Micronaut application already in, it is necessary to modify two files.

1. Add the MongoDB service 

In your .platform/services.yaml file, include MongoDB with a valid supported version:

    type: mongodb:3.6
    disk: 512

2. Grant access to MongoDb through a relationship 

In your file, use the service name dbmongo to grant the application access to MongoDB via a relationship:

    mongodatabase: "dbmongo:mongodb"

3. Export connection credentials to the environment 

Connection credentials for services are exposed to the application container through the PLATFORM_RELATIONSHIPS environment variable from the deploy hook onward. Since this variable is a base64 encoded JSON object of all of your project’s services, you’ll likely want a clean way to extract the information specific to the databse into it’s own environment variables that can be easily used by Micronaut. On, custom environment variables can be defined programmatically in a .environment file using jq to do just that:

export MONGO_PORT=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].port"`
export MONGO_HOST=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].host"`
export MONGO_PASSWORD=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].password"`
export MONGO_USER=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].username"`
export MONGO_DATABASE=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].path"`
export JAVA_OPTS="-Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError"

4. Connect to the service 

Commit that code and push. The application is ready and connected to a MongoDB instance.