Setting up MongoDB on a Node app: using mongoose and mLab.

Steven Aguilar
2 min readMay 31, 2018

--

I currently started learning about Node and found setting up up a NoSQL database to be very interesting. The two most popular types of NoSQL databases are document databases and key-value databases. Document databases excel at storing objects which makes them a natural fit for Node and JavaScript. For this blog post, we’ll be using MongoDB which is the leading document-database.

First, we’ll create an account in mLab which is aMongoDB hosting service. Once finished with the registration form and login in, you’ll be on your home dashboard. Under MongoDB Deployments click on create new and you will be taken to the following page with some options for your new database:

You can choose from any of the three Cloud Providers described on the image above. I have experience with AWS so I’ll choose it as my Cloud Provider option and Sandbox for my Plan Type (which is free). Then choose a region near you. I’ll be choosing US-EAST(Virginia). You will then be requested to name your database. In the form field DATABASE NAME type the name of your choice. The final step is to review your details and click on submit order. Congratulations! You have created your database!

We’ll be using an object document mapper (ODM). The officially supported ODM for MongoDB is Mongoose. Mongoose introduces schemas and models, which are similar to classes in traditional object-oriented-programming. Let’s start by installing the Mongoose module.

npm install --save mongoose

Then we’ll add our database credentials to the credentials.js file:

module.exports = {
mongo: {
development: {
connectionString: <YOUR_CONNECTION_STRING>,
},
production: {
connectionString: <YOUR_CONNECTION_STRING>
}
}
};

You’ll find the connection string on mLab on the home dashboard under MongoDB Deployments. Click on the deployment previously created. You’ll then be redirected to the profile of your database, where you’ll see the “connect using a driver via the standard MongoDB URI”. Copy that link which would be in the following format:

mongodb://<dbuser>:<dbpassword>@ds157571.mlab.com:57571/db_name

Now, to connect to our database, we would type the following in our app.js file:

var mongoose = require('mongoose');var opts = {
server: {
socketOptions: {keepAlive: 1}
}
};
switch(app.get('env')){
case 'development':
mongoose.connect(credentials.mongo.
development.connectionString, opts);
break;
case 'production':
mongoose.connect(credentials.mongo.
production.connectionString, opts);
break;
default:
throw new Error('Unknown execution environment: ' +
app.get('env'));
}

The options object is optional, but we want to specify the keepAlive option which will prevent database connection errors for long-running applications (like a website). There you go! You have now connected your node app to your MongoDB database!

--

--

Steven Aguilar

Developer | Baruch CIS | Rails enthusiast | Curious about Elixir