Integrate a managed MongoDB Atlas database to your application running on Koyeb and benefit from Koyeb 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 properly set the DATABASE_URL
environment variable with the connection string of your MongoDB cluster.
To create a MongoDB Atlas cluster, once you log in, 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.
The application we will deploy on Koyeb to connect to the MongoDB cluster uses Prisma as an ORM. Prisma is an open-source ORM. We use it to synchronize our database schema with our Prisma schema.
Before deploying the application, we will need to perform a migration to create the database schema and seed data.
Start by cloning the example application that will connect to the MongoDB Atlas cluster on your machine and navigate to the directory by running the following commands:
git clone git@github.com:koyeb/example-express-prisma.git cd example-express-prisma git checkout mongodb
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
To synchronize our database schema with our Prisma schema and seed data, run the following commands in your terminal:
DATABASE_URL="mongodb+srv://<user>:<password>@<host>/<database-name>?retryWrites=true&w=majority" npx prisma db push DATABASE_URL="mongodb+srv://<user>:<password>@<host>/<database-name>?retryWrites=true&w=majority" npx prisma db seed
*Make sure to replace user
, password
, host
, and database-name
with your own.
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 select the mongodb
branchwas
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 we prepared earlier. 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 mongodb \ --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 we prepared earlier. 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: