MongoDB & Express

Introduction

Traditionally data from web applications is stored in relational tables via Structured Query Language (SQL). Some of the advantages of storing data this way include custom data presentation and a large community of support. The main disadvantage to using SQL is that due to each table relating to one another it traditionally does not scale very easily. A solution for storing data that addresses this problem is a system called NoSQL.

What Is NoSQL?

NoSQL is a way of storing information through key value pairs. In practice this resembles JavaScript Object Notation (JSON). However the information is typically stored in a binary format known as Binary JSON (BSON). These key value pairs do not have to follow the strict formatting rules an SQL table would enforce. In fact data can even be repeated either partially or completely in multiple documents. A typical free NoSQL solution that you can implement within your applications is MongoDB.

Implementing MongoDB In Express

After signing up for and installing MongoDB Atlas open the Express application you’d like to use MongoDB in to store data. You are going to want to install the driver via a node package that your application will use to interact with your new database. Within terminal inside the root of your project enter:

npm install --save mongodb

Next you are going to want to create a file that will house the configuration your application will use to connect to MongoDB. I usually place this within util/database.js. Within this file I’ll first import MongoDB to access the package I just installed. Then I’ll use a method this package provides to construct a new MongoClient instance that I will call .connect() on and supply the url of my MongoDB cluster. This url is accessed via the connection setting on the MongoDB Atlas page for your cluster. You are going to want to change the password in the url to the one you used when setting up your cluster. Because the connect() method returns a promise you’ll also want to chain a .then() statement afterwards in order to initiate your connection. Once you do all of this util/database.js should look something like this:

Once this is set up and exported, the next step is to implement this connection within a model. This will allow you to perform CRUD operations on models you define using your connection to your database. For a list of full CRUD operations I suggest taking a look at the documentation. Implementing these in reference to saving and updating on an Authors model within models/author.js might look something like this:

The last step in order to use MongoDB is to import mongoConnect within the top level of your application, and initiate the connection once the server starts up. So at the top of app.js import your connection like:

const mongoConnect = require('./util/database').mongoConnect;

And then at the bottom of app.js call this connection once you’ve defined all your middleware Express will use and start the server. This should look like:

mongoConnect(() => { app.listen(3000) });

Conclusion

At this point you should have the basics down on how to implement MongoDB within an Express application. Of course there is much more information available on how to setup and interact with your NoSQL database using MongoDB within their documentation. I hope you have enjoyed this brief article and thank you for reading! Until next time happy coding.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store