First you need to go to Heroku web site and click on any of your apps.

Then go to resources and click on Find more add-ons

You can follow this link directly as well:

https://elements.heroku.com/addons

mLab MongoDB is a mongo database service that is going to let us integrate mongo db. with our heroku application.

Here you can follow previous post where we demonstrated how to deploy a simple app to Heroku

To configure mongoDB first run heroku addons:create mongolab:sandbox in terminal, where sandbox indicates we want to use the free plan.

When we run this is going to configure mlab with our Heroku application and with this we are good to go.

Next  you can run heroku config to verify configuration.

There you can see we have MONGODB_URI which is an environment variable available in hero web app in process.env when our app runs on Heroku, so we can use a similar technique we use for configuring the dynamic port.

Right now let’s say we have something like this:

let mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/DBName');

module.exports = { mongoose };

After configuring process.env.MONGODB_URI we should have something like this:

let mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI ||
    'mongodb://localhost:27017/Stocks');

module.exports = { mongoose };

This is basically just saying that if Heroku can find process.env.MONGODB_URI will use it if not it will use ‘mongodb://localhost:27017/DBName’.

Finally, all we have to do is to commit our changes and push to Heroku by using command:

git push heroku master