Getting started with using MongoDB with free Heroku account

Deepjyoti Barman @deepjyoti30
May 11, 2020 12:00 AM UTC
Post cover

I have been running away from using a proper Database for my personal page for a while. I thought it would be a lot of hassle considering that my backend is present on Heroku and the frontend is hosted using Github Pages, I was wrong.

I recently was working on a project with some of my friends, it was for a Hackathon I participated in. In that, I came through a problem of using a database in order to store the data of the users. I initially did not put much thought on it, however people suggested that I use a graph database for that problem, it would blend in perfectly for my situation.

I put some thought on it and researched a bit. I ended up using neo4j as my backends graphDB. Surprisingly, the process to use it was really simple and as usual I used it with Heroku (I might write about that later in a different post).

So now I thought, why not use a database for my personal page as well. I had time and I thought it would be better if I would do that since I'm also thinking about moving away from github pages and buying my own domain (not sure).

So that's that, I finally made the move to a DB. As of today, all the endpoints are working with the newly implemented logic to work with the database. As usual I thought why not write a blog about it.

How it works

In order to host a database, Heroku uses MongoDB's mLab. It is actually a cloud service provided by MongoDB themselves that allows users to create databases in the cloud and users can connect to them using the URL provided by them.

We will have our backend code hosted on Heroku and our database hosted on some server handled by mLab.

Usually if a person wants to use a database on their own server, than the server can be installed locally and then run on a port and the code that will interact with the server will connect to this local port.This is almost similar to using our own server with the difference that the database will be handled by mLab.

So to conclude, we will have our backend code hosted on Heroku and our database hosted on some server handled by mLab. Our code present on Heroku will interact with the database present in mLab's servers through their URL and write or read documents.

Getting Started

First things first, make sure you have a app created on Heroku where all the backend code will go. Once that's ready we can run one command to install the add on on to the app.

heroku addons:create mongolab

Alternately it can also be added through the add-on catalog.

After the app is added, get the URL that will be used to connect to the mongoDB database. It can be get by the following command.

heroku config:get MONGODB_URI

What happens is, once the app is added, Heroku creates a new database for you and the URL to connect to that database is added as a environmet variable that can be fetched by the above command.

Once this is done, you can go to mLab and created databases, collections, users etc everything right from there.

Now that we have the database set up, how do we talk with the database in order to store our data. So that can be done by various. You can install mongodb-shell in order to be able to connect to the database from your local terminal.

The preferred way to interact is obviously a language. I like to use Python, however MongoDB has drivers for a lot of languages and a lot of documentation is also available for each one of them.

For Python the module is pymongo that is available in PyPi and is pretty straightforward to use. I used this module for all the stuff I'm doing in order to connect to the remote database.

Conclusion

Thats it people, it's that simple to setup and use mongoDB from Heroku and I was seriously blown away by how simple it is.

However, while storing data take precautions. Try to follow best practices in order to keep your database secure. As I read in one Stackoverflow answer, if we make a different user that has access to just one collection (tables are collections in MongoDB), even it's better than not having any security and just using the root user to connect.

Also try to refer to the official docs as the methods, classes keep changing with updates. I myself faced an issue because I was trying to connect' and write to the database in a method that was depreciated, so try to always read the docs and follow those rather than following some random article.

Discussion