Sunday, April 3, 2016

Understanding the MEAN stack - Part 3 : MongoDB

We are half-way through the MEAN stack right now. We have discussed Node JS and Express JS and now let us discuss the persistence tier in the stack i.e. MongoDB.
So we now have a platform in place provided by node and a web server in place provided by express which as we discussed largely provides us with our restful end-points. But those restful end-points are no good unless they actually have some persistence associated and that is what MongoDB brings to the table.
Let us take a look at some features of MongoDB:

  • Non-relational - It does not enforce expectations and constraints like ACID properties on the data as relational databases do.
  • Scalable - It is horizontally scalable. MongoDB employs a method called Sharding in which it divides the data set over multiple servers. 
  • Highly available - It is able to tolerate multiple failure nodes and still be fully available i.e. deliver reads and writes operations.
  • Document database - Instead of storing rows in tables MongoDB stores documents formatted in JSON. Similar structured documents are collectively organized into collections.
  • Flexible - Since it is a simple JSON store you can initially start off with what you think your data model looks like and later change this without affecting the documents. This gives true iterative development capabilities.
So why MongoDB in the MEAN stack?

As we said earlier MongoDB brings persistence to our MEAN stack application. But why choose MongoDB over a relational database like MySQL. Node supports MySQL with the node_mysql module. So far we have seen javascript in both the tiers of the MEAN stack and it is also know that Angular.js is a javascript framework. So why take the trouble of having to introduce another language like SQL when you have the other tiers of your application in javascript. 
  • With MongoDB you can have javascript throughout your application and this increases the productivity of the developers working on the application. 
  • Querying data from NodeJS and ExpressJS based server and passing it to the frontend (AngularJS) becomes lot easier since we are storing data in JSON format in MongoDB.
  • Another important advantage of using MongoDB is that it provides flexibility and in a sense a degree of freedom when designing the schema/data model. We can start off with a rough model with what information we have and let it evolve through an iterative and agile approach. To give you an example I was building the todo application and initially I started off with something as simple as the following:
mongoose.model('Todo', {
    text : String, // The textual content of the todo item e.g.:"Pay bills"
          Later when I wanted to implement a feature to mark a todo item as active or completed I had to add a new property to the todo model to keep track of the status
mongoose.model('Todo', {
    text : String, // The textual content of the todo item e.g.:"Pay bills"
    active: Boolean // Active status of the todo item. true=>active, false=>completed
All that said MongoDB has its own set of cons too like any other technology. It is true that MongoDB is fast and scalable but that comes at the cost of strong consistency and durability which any of our traditional relational databases offer.

Oops! I almost forgot to mention about Mongoose. You must have seen in the above code snippets that I have used an object called mongoose to describe my todo data model. Mongoose is a library that helps in providing a structure to a schema-less datastore like for MongoDB along with some other important features like:
  • Validation.
  • Default data types.
  • Eventing - pre and post save hooks (middlewares)
  • Indexes.
  • One-to-Many relationships.
  • Pseudo-joins - populate method
Here is nice resource to learn more about the features Mongoose bring to the table.