Request-Response

The Full-Stack Blog

Deploy with Render and MongoDB Atlas

February 06, 2024

This guide walks you through the steps required to deploy your application to Render with a MongoDB database using MongoDB Atlas.

If you don't yet have a MongoDB Atlas account, please see Set Up MongoDB Atlas before you proceed.

Create a Database on MongoDB Atlas

First, you'll create a database for your application.

  1. Navigate to the MongoDB Atlas dashboard. Click on the "Database" link in the left-hand sidebar. You'll see something like the following image:

    The MongoDB Atlas dashboard shows the clusters we've created.

  2. To create a database for your application, click the "Browse Collections" button in your sandbox Clusters box. If you haven't previously created a database, you'll be taken to a page that looks like the following image:

    The MongoDB Atlas cluster shows that no databases have been created yet.

  3. From this page, select the "Add My Own Data" button. If you previously created a database through MongoDB Atlas and need to create another one for this app, click the "+ Create Database" button in the left column of the window pane instead. Either way, the resulting modal should look like the following image:

    The Create Database modal dialog allows users to create a database name and starter collection.

  4. Fill out the form with the name of your MongoDB database and a collection for that database. Leave the "Additional Preferences" field blank. You only need to create one collection to get started, as your application will create them upon deploy, so don't worry if you think your database will scale up or down in the future.

  5. When you're done creating your database and initial collection, the dashboard should display them, as shown in the following image:

    The collections tab shows the database and collection you just created.

Great! Your database has been created. Let's move on to connect it to your application in production.

Project Setup

You've likely already deployed a front-end application to a platform like GitHub Pages. But to deploy an application with a back end, you'll need a platform like Render that can handle the additional configurationso that your app can accept incoming connections from the internet.

To start, make sure that your project is initialized as a Git repository. You can check this by running the following command at the root of your project:

git status

If you encounter an error, that means that your project isn't yet a Git repository. You can initialize the repository by running the following command:

git init

Applications that feature an Express.js back end can use Render's PORT environment variable. To set this up, create a port variable with a value of process.env.PORT. You can also add a default value for local instances of your server by using the || syntax, as shown in the following example:

const port = process.env.PORT || 3001

Render allows you to deploy directly from a GitHub repository, so make sure you have committed and pushed your changes to your GitHub:

git add -A
git commit -m "<descriptive message here>"
git push

Now that we've created the repository, configured the server, and pushed to GitHub, we can create an app on Render and link it to our existing GitHub repository.

Create a Render App

The Render dashboard allows us to easily create an app and link an existing GitHub repository to it to deploy.

Create a new Render app by clicking the "New" button on the right side of the navbar, as shown in the following image:

Screenshot of the Render dashboard. A button with the text "New +" is on the right side of the navbar.

Select "Web Service" from the dropdown menu, as shown in the following image:

Screenshot of the Render dashboard after clicking on the "New +" button. A dropdown menu appears with several options. The second choice has the text "Web Service".

Make sure "Build and deploy from Git repository" is selected, then click "Next," as shown in the following image:

Screenshot of the Render page for building a new Web Service. Two radio buttons exist in a form; the first radio button has the text "Build and deploy from Git repository." A button with the text "Next" is toward the bottom of the page.

If this is your first time deploying to Render, you will need to allow access to your GitHub repositories. Click on the "+ Connect account" link under the GitHub heading, as shown in the following image:

Screenshot of the Render create web service page. A link with the text "+ Connect account" is on the Right side of the page, under a heading with an icon of the Github logo and the text "GitHub".

Next, install Render to your GitHub account, providing access to all of your repositories, as shown in the following image:

Screenshot of the GitHub allow permissions page. A radio button with the text "All repositories" is selected in the middle of the page, and a button with the text "Install" is towards the bottom of the page.

Click the blue "Connect" button next to the repo you want to deploy. You can use the search box to filter out the repo you want, as shown in the following image:

Screenshot of the Render dashboard after connecting to GitHub. the term "fluff" has been entered into an input field toward the top of the page, and a button with the text "Connect" appears to the right of the filtered repository names.

Give your new Render app a unique name, as shown in the following image. It does not have to match the repo name, but it is useful to do so to keep track of which app is attached to which repo:

Screenshot of the Render create web service page. An input with the label "Name" is at the top of the page and has been given the value of "fluffy-spork".

Render needs to know which steps need to be taken to get your app up and running. In the case of an Express.js app, we need to run npm install to install all required dependencies. Scroll to the section "Build Command" and update the field to npm install. Render also needs to know which command to run to start your application. The "Start Command" field will be automatically populated with the start script from your package.json. Confirm that the "Start Command" is correct, as shown in the following image:

Screenshot of the Render create web service page partially scrolled from the top. An input with the label "Build Command" has been given the value of "npm install," and an input with the value of "start command" has been given the value "node index.js".

Make sure you leave the free tier selected, as shown in the following image:

Screenshot of the Render create web service page partially scrolled. A collection of radio buttons is displayed with various pricing options. The first radio button, with the text "Free", is selected.

Next, click the "Create Web Service" button, as shown in the following image:

Screenshot of the Render create web service page scrolled to the bottom. A button with the text "Create Web Service" appears at the bottom of the page.

The next page will log all the actions being taken to set up your server. Once your server goes live, you can use the provided link toward the top of the page to view your live site, as shown in the following image:

Screenshot of the Render web service instance page. A link to the live deployed site appears on the top left of the screen.

Clicking the "Environment" link takes you to a page where you can set your environment variables, as shown in the following image:

Screenshot of the Environment page.

Open your MongoDB Atlas dashboard in another browser tab so you don't leave the Render page. Once there, locate the Connect button in your cluster's information and click it. If you're having trouble finding it, refer to the following image:

The MongoDB Atlas dashboard shows our cluster with a connect button in it.

When the connection modal dialog opens, you should see the options shown in the following image:

The connection modal presents three options for connecting to our database.

Because you want to connect our database to an application, select the first option, "Drivers," under the header "Connect your application." You'll then see something like the following image:

The connection modal now shows instructions for us to connect our database to our application.

Here, all you need to do is copy the connection string listed in the second step. So go ahead and click the Copy button.

With the connection string copied, navigate back to your Render application settings.

Click the "Add Environment Variable" button, add a key named MONGODB_URI and paste the value of the connection string.

We need to update the connection string to include our database name, username, and password. The URL connection string should look like the following code:

mongodb+srv://<username>:<password>@cluster0.5k55w.mongodb.net/<dbname>?retryWrites=true&w=majority

By default, MongoDB Atlas will populate your database username and leave off the database name, as shown in the following code:

mongodb+srv://twoser:<password>@cluster0.dfd4e4s.mongodb.net/?retryWrites=true&w=majority

Update the string so your password is correct and make sure to add the database name where <dbname> is indicated above, as shown in the following code:

mongodb+srv://twoser:password1234@cluster0.5k55w.mongodb.net/firstdb?retryWrites=true&w=majority

Note Make sure you're using the database user password, and not your MongoDB Atlas account password.

Click the "Save Changes" button to apply the changes, as shown in the following image:

Screenshot of the Environment Variables page.

Finally, you can click the "Logs" link and use the provided link toward the top of the page to view your live site, as shown in the following image:

Screenshot of the Render web service instance page. A link to the live deployed site appears on the top left of the screen.

Resources

Visit the following link to supplement this guide:

This page was updated 6 months ago
© 2022 edX Boot Camps LLC. Confidential and Proprietary. All Rights Reserved.

Category: MongoDB

Tagged under: mongodb, mongodb atlas, deployment, render, guide,

All Posts