MongoDB vs RDBMS and it’s Advantages

Reading Time: 5 minutes

In this blog, I will introduce you with the MongoDB and will draw a comparison with RDBMS and highlight its advantages over the later. MongoDB is a No SQL database.  It is an open-source, cross-platform, document-oriented database written in C++.

MongoDB is developed by the company named 10gen. They define MongoDB as: “Mongo DB is scalable, open source, high performance, document-oriented database.”

Why MongoDB?

In this section, we will discuss the advantages of MongoDB:

  • Rich Object Model
  • Secondary Indexes
  • Replication and high availability
  • Native Aggregation
  • Schema-less Models

Let us discuss each of these in details.

  • Rich Object Model: MongoDB supports a rich and expressive object model. Objects can have properties and objects can be nested in one another (for multiple levels). This model is very “object-oriented” and can easily represent any object structure in your domain.  You can also index the property of any object at any level of the hierarchy – this is brilliantly powerful!
  • Secondary Indexes: Indexes speed up the queries significantly but they also slow down writes. Secondary indexes are a first-class construct in MongoDB. This makes it easy to index any property of an object stored in MongoDB even if it is nested. This makes it really easy to query from the database based on these secondary indexes.
  • Replication and high availability: MongoDB supports a “single master” model. This means you have a master node and a number of slave nodes. In case the master goes down, one of the slaves is elected as master. This process happens automatically but it usually takes time, before the 3.2 release,  10-40 seconds were taken but after the release of MongoDB 3.2 and later, failures are detected faster and a new leader elected in under 2-10 seconds. The trade-off for multi-master is that the reads are slower and scale less effectively because the client must read from multiple nodes to ensure consistency. During the time of new leader election, your replica set is down and cannot take writes.
  • Native Aggregation: MongoDB has a built-in Aggregation framework to run an ETL(Extract, transform and load) pipeline to transform the data stored in the database. This is great for small to medium jobs but as your data processing needs become more complicated the aggregation framework becomes difficult to debug.
  • Schema-less Models: MongoDB, allows you to not enforce any schema on your documents. While this was the default in prior versions, in the newer version you have the option to enforce a schema for your documents.  Each document in MongoDB can have a different structure and it is up to your application to interpret the data. While this is not relevant to most applications, in some cases the extra flexibility is important. Schema-less models mean that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

Let’s take an example for getting more clarity when should you prefer MongoDB over RDBMS :

In RDBMS, defining the schema before inserting any record is mandatory. And the structure of the record been inserted should strictly match with the schema.

RDBMS: Defined the schema for student Table.

Now, let’s try to insert the record

Now, suppose I want to add one more field in student table. So for that firstly I have to alter the schema of student table to do that.

Let’s try without altering the schema:

This will result in an error as we already discussed.

MongoDB:

Let’s try the same thing with MongoDB :

Here we don’t need to create the schema. There is no concept of a table. In Mongo, we insert the record in the collections. And we don’t need to define any schema structure for any operation.

Let’s try to insert:

Here, I am inserting in the student collection.

Screenshot from 2018-06-25 12-25-57

Now let’s try to insert the record in student collection with different schema.

Screenshot from 2018-06-25 12-30-41

Now, try to fetch all records.

Screenshot from 2018-06-25 12-31-14

So, this is how MongoDB works.

Advantages of MongoDB over RDBMS:

MongoDbAdvantages

  • MongoDB is schema-less. It is a document database in which one collection holds different documents. But in RDBMS firstly need to design your tables, data structure, relations first and then only you can start coding.
  • There may be the difference between the number of fields, content and size of the document from one to other. But in RDBMS every record must adhere to a  particular predefined schema.
  • MongoDB is Horizontally scalable i.e we can add more servers(sharding) but RDBMS is only vertically scalable i.e increasing RAM.
  • MongoDB emphasizes on CAP theorem (Consistency, Availability, and Partition tolerance) but RDBMS emphasizes on ACID properties (Atomicity, Consistency, Isolation and Durability).
  • MongoDB is best suitable for hierarchical data storage but RDBMS is not suitable for hierarchical data storage.
  • MongoDB supports JSON query language along with SQL but RDBMS supports SQL query language only.
  • MongoDB is easy to set up, configure and run in comparison to the RDBMS. It’s Java client is also very easy to use.
  • MongoDB is almost 100 times faster than traditional database system like RDBMS which is slower in comparison with the NoSQL databases.
  • There is no support for complex joins in MongoDB but RDBMS supports complex joins which can be difficult to understand and take too much time to execute.
  • It uses internal memory for storing working sets resulting in faster access time.
  • MongoDB supports deep query-ability i.e we can perform dynamic queries on documents using the document-based query language that’s nearly as powerful as SQL.
  • In MongoDB, Conversion/mapping of application objects to database objects is not needed.

Hope this blog will help you to understand the use case of MongoDB and its advantages over the traditional RDBMS.

Happy Blogging. Image result for smiley face pictures

References: https://scalegrid.io/blog/cassandra-vs-mongodb/

knoldus-advt-sticker

 

Written by 

Kunal Sethi is a Software Consultant with experience of more than 2.5 years. He is a Java enthusiast and has knowledge of various programming languages such as Scala, C++. He is familiar with Object-Oriented Programming Paradigms, loves to code applications in J2EE. He developed his own stand-alone application in Java based on the intranet chatting System and email system during his Master's Degree.

8 thoughts on “MongoDB vs RDBMS and it’s Advantages5 min read

  1. Actually This post is exactly what I am interested. we need some more good information. Please add more good information that would help others in such good way.
    Can we do MongoDB replication and Sharding in different OS ?

  2. Thank you for awesome blog. I followed each and every step. and it is successful.
    Such a very useful article. Very interesting to read this.I would like to thank you for the efforts you had made for writing this awesome article.

  3. Thanks for the thorough explanation.Good summary of a simple but often miss understood topic 🧡 This blog is the fascinating one and it induces me to know more about it
    Thanks for the sharing this blog and keep on sharing these kinds of useful blog.

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading