MongoDB vs. RDBMS
Let's look at one man's opinion on why MongoDB has more advantages over other databases such as RDBMS.
Join the DZone community and get the full member experience.
Join For FreeIn this article, I will introduce you to MongoDB and draw a comparison with RDBMS and highlight its advantages over the latter. 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 a scalable, open source, high performance, document-oriented database."
Why MongoDB?
In this section, we will discuss the advantages of MongoDB:
Let us discuss each of these in detail.
- 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 look at 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 being inserted should strictly match with the schema.
RDBMS: Defined the schema for student Table.
CREATE TABLE student(
id bigint PRIMARY KEY NOT NULL,
name TEXT)
Now, let's try to insert the record
Insert into student values (1,"Kunal");
Now, suppose I want to add one more field in the student table. For that, I have to alter the schema of the student table to do that.
Let's try without altering the schema:
mysql> Insert into student values (1,"Kunal","kunal@gmail.com");
ERROR 1136 (21S01): Column count doesn't match value count at row 1
This will result in an error as we already discussed.
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 MongoDB, 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.
Now let's try to insert the record in student collection with different schema.
Now, try to fetch all records.
So, this is how MongoDB works.
Advantages of MongoDB over RDBMS:
- MongoDB is schema-less. It is a document database in which one collection holds different documents. But in RDBMS, you need to first design your tables, data structure, relations, and only then can you start coding.
- There may be a 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 the CAP theorem (Consistency, Availability, and Partition tolerance) but RDBMS emphasizes ACID properties (Atomicity, Consistency, Isolation, and Durability).
- MongoDB is best suitable for hierarchical data storage, but RDBMS is not.
- 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.
- MongoDB 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.
I hope this article will help you understand the use case of MongoDB and its advantages over the traditional RDBMS.
Happy Blogging.
References: https://scalegrid.io/blog/cassandra-vs-mongodb/
This article was first published on the Knoldus blog.
If you enjoyed this article and want to learn more about MongoDB, check out this collection of tutorials and articles on all things MongoDB.
Published at DZone with permission of Kunal Sethi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments