Table of contents
1.
Introduction
2.
Set up MongoDB Atlas
2.1.
Sign-up Process
2.2.
Create a Database
2.3.
Add New Database User
2.4.
Add an IP Address
3.
Connect Database to Heroku
3.1.
Set Up Heroku Variables
4.
Update Application Code
5.
Frequently Asked Questions
5.1.
What is MongoDB?
5.2.
What is Heroku?
5.3.
Does MongoDB support SQL?
5.4.
What is a BSON document in MongoDB?
5.5.
Is Heroku good for beginners?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Deploy MongoDB on Heroku

Author Sohail Ali
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

MongoDB is one of the most popular NoSQL databases. It is being used to store the data in a document format where this document resembles a JSON object. Heroku is a cloud platform that is extensively used to develop and deploy web apps in various languages. So learning how to connect Heroku with MongoDB becomes important.

How to Deploy MongoDB on Heroku

In this blog, we will deploy an application on the Heroku platform with a MongoDB database. So let’s get started!

Set up MongoDB Atlas

Follow the given steps properly to set up your MongoDB Atlas account.

Sign-up Process

Step 1: Create an Atlas account. Navigate to the MongoDB atlas sign-up page and create your account. If you already have an account, then you can click on the Sign In option.

Sign-up Process

Step 2: Here you can sign-up either using your email id or your Google account. Fill in all the required fields properly and check the privacy policy and click on Get started free.
 

Step 3: Once you verified your email address, you are done creating your account. Next, head back to the login page and log in to your account. 
If you are a first-time user of this platform, you will be given the ‘Welcome to Atlas’ page.

Welcome to Atlas

Step 4: Choose the options according to your requirement and click on the Finish button.

Create a Database

Step 5: Now, you will be taken to create a database to start using MongoDB Atlas.
You can choose the Auto Setup field, which will automatically do your Atlas setup, or you can opt for the Manual field, where you can configure your deployment manually with some advanced configuration options.

Create a database

Step 6: Here, we will choose the I’ll do this later option and then add a new database user from scratch. 

Add New Database User

Follow the below steps correctly to add a new database user:

Step 7: On the left-hand navigation menu, click on the Database Access link to which the below image will pop up.

Add New Database User

Step 8: Fill out the complete form with the given steps:

  • First, in order to authenticate your database user, you need to authenticate your system. 
     
  • Add your username and unique password. Make sure to add a strong password that you’ll remember. You can even autogenerate your password.
     
  • Now, under the Built-in Role section, select one built-in role for the user. 
Build-in Role section
  • Select the Atlas admin option.

    Note: Do not check the Temporary User option. It would make your user a temporary user, which will be deleted after the specified time.
     

Step 9: Whenever you are done, click on Add User button.

Add an IP Address

Follow the below steps to allow your IP address:

Step 10: On the left-hand menu, navigate and click on the ‘Network Access’ link.

Add an IP Address

Step 11: Click on ALLOW ACCESS FROM ANYWHERE, which will set the value to 0.0.0.0/0
 

Step 12: Click on Confirm button.

Congratulations! You have successfully set up your first Atlas cluster.

Connect Database to Heroku

Now, let’s connect our Heroku application to the MongoDB Atlas database. 

Set Up Heroku Variables

First, let us add the connecting string of the database to the Heroku environment variable.

Step 1: Go to your MongoDB Atlas dashboard and click on the connect button from your cluster information.

Connect Database to Heroku

Step 2: Now choose the “Connect to your application” option.

Connect to your application

Step 3: You will see the connection string, and then click on the copy button.

Connection string

Step 4: Let us now create the Heroku app for your project. Navigate to your applications directory and type the below command.

heroku create

 

Step 5: Now go to the Setting tab of your Heroku app. Go to the Config Var section, where we need to configure the Config Vars. You will see the below image.

Config Vars

Step 6: Now add MONGODB_URI in the first box. Now remove the quotes from your connection string which we got in previous steps; it will look some like below.

mongodb+srv://YourUsername:YourPassword@YourClusterName.k6b23.mongodb.net/MyApp?retryWrites=true&w=majority

 

Step 7: Click on the Add button and then save your new Config Vars.

Great going! Now, let’s complete the last step of our deployment, where we need to update our application code.

Update Application Code

Follow the below steps to update your application’s code.

  • Below are the codes of our application.
     

HTML

<header>
    <h1>Welcome to my Application</h1>
    <p>We have successfully deployed our app.</p>
    <link rel="stylesheet" type="text/css" href="style1.css">
</header>
<br>
<body>
    <nav id="nav">
        <ul>
            <p>Well Done Ninjas!</p>
            <form>
                <p>Give us a feedback</p>
                <textarea></textarea>
                <br>
                <input type="submit">
            </form>
        </ul>
    </nav>
</body>

    

CSS

header{
    font-family: 'Dancing Script';
    text-align: center;
    border: 3px solid #2E2E2E;
    padding: 5px;
    width: 400px;
    margin: 0px auto;
    box-shadow: 0px 0px 15px #434F5B;
}
header h1{
    color: #black;
    font-size: 45px;
}
header p{
    color: black;
}
#nav ul{
    padding: 20px 15px;
    margin: 0px;
    text-align: center;
}

 

  • Go to the IDE, where you have written the code of your application. Find where you connect to your database and update it. It will look similar to the code given below:
     
mongoose.connect(process.env.MONGODB_URI || 'mongodb://IP_Address/Databse_name');

 

Note: You may require to install dotenv npm package in case of your Javascript application (node.js).

Command to install dotenv:

npm install dotenv

 

  • After importing the package, your application code will look like below.
     
// app.js
require('dotenv').config()

// Rest of your code

const port = process.env.PORT || 3000;
app.listen(port);
You can also try this code with Online Javascript Compiler
Run Code

 

  • Save your code and use the below Git command from your local main branch to add, commit and push it to Heroku.
     
git add -A
git commit -m 'Deploy'
git push heroku main

 

  • Check whether your application is deployed correctly by using heroku_open, which will open your app in your default browser.
     

If every step is followed correctly, you will get your desired output.

Final output

Now, whenever the user enters new entry feedback, it will get updated on our MongoDB Atlas database.

Database update

Wooh! Finally, we have successfully deployed MongoDB on the Heroku platform.

Frequently Asked Questions

What is MongoDB?

MongoDB is a popular NoSQL database. It is an open-source document-oriented database that is used in the management of all sorts of data.

What is Heroku?

Heroku is a cloud platform service that is used to build, deliver, and monitor apps. 

Does MongoDB support SQL?

No, MongoDB does not support SQL. MongoDB has its own query language based on Javascript.

What is a BSON document in MongoDB?

BSON stands for Binary JSON, which is the data format used by MongoDB to organize and store the data.

Is Heroku good for beginners?

Yes, the Heroku platform is very useful and super easy in order to launch your web apps quickly and free of cost.

Conclusion

This article discusses how to deploy MongoDB on the Heroku cloud platform. We hope this blog has helped you enhance your knowledge about the Heroku cloud platform and its services. If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your coding ability, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass