Deploying apps using MongoDB to Heroku

To deploy your application that is using MongoDB to Heroku you need a MongoDB server running in the cloud. mLab provides hosted MongoDB servers in the cloud.

Use mLab to deploy your application:

In your application:

Use code like this:


const mongoURL = process.env.MONGO_DB_URL || "'mongodb://localhost/test'";

mongoose.connect(mongoURL);

In Heroku:

Add an Config Variable under the Settings tab. Enter a key and value.

The key should be, MONGO_DB_URL the value should be something like, mongodb://<mongo_username>:<mongo_password>@yourinstance.mlab.com:62059/db-name

After setting this up your application on Heroku should be up and running using MongoDB. You might need to restart your application in Heroku.