Integrate a managed MongoDB Atlas database to your application and benefit from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.
MongoDB Atlas
This guide explains how to connect your MongoDB Atlas cluster to an application running on Koyeb. To successfully follow this documentation, you will need to have:
If you already have a MongoDB cluster running and want to quickly preview how to connect your MongoDB cluster to an application running on Koyeb, use the Deploy to Koyeb button below.
Make sure to replace the DATABASE_URL
environment variable with the connection string of your MongoDB cluster.
To create a MongoDB Atlas cluster, you log in to your account. Select the organization and find the project you want to use. Then, click on the Build a Database button.
us-east-1
as our Koyeb service will be deployed in this regionmongodb-koyeb
Once you have completed the cluster configuration, click on the Create Cluster button.
Next, we need to configure how to authenticate the connection to the cluster. We have two options:
We will use the first option which will create a user using a username and password.
koyeb
With the user created, we are now ready to connect to the cluster and can move to the next step where we will show how to connect your MongoDB Atlas cluster to an application running on Koyeb.
On the MongoDB Database Deployments page, click on the Connect button of the cluster you created. A modal open and ask you to choose the connection method. Select Connect your application and copy the connection string similar to the following:
mongodb+srv://<user>:<password>@<host>/?retryWrites=true&w=majority
Save it in a safe place, we will need it in the next step to connect our application to the database.
You can deploy on Koyeb using the control panel or via the Koyeb CLI.
To deploy the example application using the
control panel
github.com/koyeb/example-express-prisma
as the GitHub repository to
deploy and keep the default branch main
npm run mongodb:init
as the Build commandwas
DATABASE_URL
environment variable to indicate the application how to
connect to the MongoDB cluster. Give the environment variable a name,
DATABASE_URL
and enter the connection string from earlier as the
valueexpress-mongodb
Koyeb is now building the application. Once the build has finished, you will be able
to access your application running on Koyeb by clicking the URL ending with
.koyeb.app
.
The example application exposes a /planets
endpoint that you can use to list
planets from the database. Once your deployment is live, you should see the
following results when navigating to https://<YOUR_APP_URL>.koyeb.app/planets
:
[ { "id": "63cef0c77cc8d4d669448fe0", "name": "Mercury" }, { "id": "63cef0c87cc8d4d669448fe1", "name": "Venus" }, { "id": "63cef0c87cc8d4d669448fe2", "name": "Mars" } ]
To deploy the example application using the Koyeb CLI, run the following command in your terminal:
koyeb app init express-mongodb \ --git github.com/koyeb/example-express-prisma \ --git-branch main \ --git-build-command "npm run mongodb:init" \ --ports 8080:http \ --routes /:8080 \ --env PORT=8080 \ --region was \ --env DATABASE_URL="mongodb+srv://<user>:<password>@<host>/mongodb-koyeb?retryWrites=true&w=majority"
Make sure to replace user
, password
, and host
with your own.
Access deployment logs
To track the app deployment and visualize build logs, execute the following command:
koyeb service logs express-mongodb/express-mongodb -t build
Access your app
Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:
$ koyeb app get express-mongodb ID NAME STATUS DOMAINS CREATED AT b60982b7 express-mongodb STARTING ["express-mongodb-ed.koyeb.app"] 24 Jan 23 10:06 UTC
The example application exposes a /planets
endpoint that you can use to list
planets from the database. Once your deployment is live, you should see the
following results when navigating to https://<YOUR_APP_URL>.koyeb.app/planets
:
[ { "id": "827696777012871169", "name": "Mercury" }, { "id": "827696777225666561", "name": "Venus" }, { "id": "827696777389539329", "name": "Mars" } ]
To delete the example application and the MongoDB cluster and avoid incurring any charges, follow these steps: