Guide for using MongoDB and deploying to Heroku


#1

In this guide let's see how to work with MongoDB locally and with mLab for deploying it to Heroku. Alternatively you can also use mLab add-on in Heroku , It is free but it may require your credit card details. So, if you are not intrested in providing your credit card details you can go with mLab website.

Setting up a free account on Heroku and mLab:

Sign up for Heroku and mLab

Starting your Mongodb(Locally):

To start your DB on your own system execute the following command:

# Create a directory named 'data' if it doesn't exist
$ mongod --port 27017 --dbpath=./data

Now your Db is running at-

mongodb://localhost:27017/my_database_name

If you are using c9 and If you are having trouble starting your DB in C9 refer to this page

Starting your Mongodb(mLab):

  1. After Creating your mLab Account, click on Create new button and select Single-node -> Sandbox to get the free Db and give your database a name (I've created a db named 'food' for this as an example) .
  2. Now a database is created with the name 'food',You can create a new collection of your own.
  3. Finally Add a User/Users who can access this Database,While Adding a user it will ask for username and password which are used to access the Database.

Now your DB is running at url something like this -

mongodb://username:password@ds01316.mlab.com:1316/food

where username and password are the details that you've given when you added a user.

You can find your DB url at https://mlab.com/databases/your_database_name

Making a Connection with MongoDB in Node.js (While DB is running on your Local System):

To work with the database, First you need to create a connection. In this section, we will be using MongoDB's native Node.js driver to create the connection with the MongoDB server. To install the mongodb native drivers, use the npm command to install the mongodb module. After that, run the following command in your project directory.

npm install mongodb

Basic Code for connecting to MongoDB

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.

//(Focus on This Variable)
var url = 'mongodb://localhost:27017/my_database_name';      
//(Focus on This Variable)

// Use connect method to connect to the Server
  MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    console.log('Connection established to', url);

    // do some work here with the database.

    //Close connection
    db.close();
  }
});

For more examples to work with MongoDB you can refer this blog

We need to know where our mongodb server is running. The url represents the location where the mongodb server instance is running such that we can connect to it. The url contains the database name to which we intend to connect.

Assuming that your database is running on the url mentioned above, let us now focus on the Url connecting the Database(local)

var url = 'mongodb://localhost:27017/my_database_name';

Making a Connection with MongoDB in Node.js (While DB is running in your mLab):

The url for connecting to mLab DB looks like this

var url = 'mongodb://username:password@ds01316.mlab.com:1316/food';

You can replace the url variable with this and everything will be working exactly the way it should be and finally your database is safe and secure at mLab where you can view your collections,users,backups etc..

Important Note:

But Commiting your username and password to your public repo is sometimes very dangerous so never commit them into public repositories, Instead you can use environment variables to store the url (containing username and password) , to do this in your local system

For Mac/Linux users, you can simply type:

export MONGOLAB_URI="mongodb://username:password@ds01316.mlab.com:1316/food"

For Windows users:

SET MONGOLAB_URI=mongodb://username:password@ds01316.mlab.com:1316/food

After setting the Environment variables you need to call the Environment Variable into your code. You can do it by typing this

var url = process.env.MONGOLAB_URI;

Now your MongoDb url is inserted into your code safely. You can now commit it and deploy it to your heroku.

If you need more help how to deploy into Heroku you can refer this Wiki

Final Steps:

After Deploying your code to your Heroku App, you need to set the environment variable for the Code in heroku.

To do this, you need to run the following command from your heroku remote,

heroku config:set MONGOLAB_URI=mongodb://username:password@ds01316.mlab.com:1316/food

Thats it, Your app is now successfully deployed in heroku with mLab DB


Issue: Heroku as webhost and uploading mongo
Where did the FCC wiki articles go? The monogdb /heroku one?
Question about local databases and pushing to Heroku
Exposing API keys
#3

I've written this as a follow up to this excellent article. This was the next step for me. I appreciate any proofreading, fact checking or edits.


#4

Thanks for the post, but I just find this article not comprehensive enough.

If I’m not wrong, “url-shortener” project is the first time we have to provision a database to heroku. Could you write a tutorial to show us how exactly do we go about doing it?


#5

What have you tried so far? I found this article to be enough for me to do it. What issues are you having?


#6

Hi, thank you so much for the response. I followed the step exactly: deploying to heroku, verified the format of user and password in the url string etc, but when I open the app on heroku, I got this error message

//lets require/import the mongodb native drivers.
var mongodb = require(‘mongodb’);

//We need to work with “MongoClient” interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.

//(Focus on This Variable)
var url = ‘mongodb://user1:123456@ds139959.mlab.com:39959/url-shortener’;
//(Focus on This Variable)

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log(‘Unable to connect to the mongoDB server. Error:’, err);
} else {
console.log(‘Connection established to’, url);

// do some work here with the database.
//Close connection
db.close();

}
});

Problem 2:
I followed another tutorial to do the url-shortening project, now I’ve been stuck at trying to provision the database unto heroku for days.
I understand that this tutorial from freecodecamp is showing just how to connect to the database, but I just couldn’t resolve the authentication problem after connected to mongolab shell. Had been trying to insert a document in the collection but it seems that all my commands are not working because, after checking through the answer from stackoverflow the user role is not set appropriately. The issue is: i try to type in the command into the shell

use admin
db.createUser(
{
user: “admin”,
pwd: “password”,
roles: [ { role: “root”, db: “admin” } ]
}
);
exit;

yet it still wont change anything because of the same issue: not authourised. How should I configure it?


#7

Let me look at mybcode and I’ll see if I can help further. In the meantime maybe browsing the follow up I wrote above can help?


#8

I’ve tried numerous other tutorials on provisioning mongolab as the database to heroku, yet there is still no luck. Here is the update of my question in stack overflow.

By the way, where would I be able to see the console.log messages as described in the codes? I know how to see the console.log messages if I run mongodb locally, but not sure of how if I am using mongolab


#9