Computer scienceFundamentalsSQL and DatabasesDBMSMongoDBIntroduction to MongoDB

MongoDB VS Relational databases

9 minutes read

Few of us would question the fact that databases have been the main tool of data collection, storage, and processing for the last few decades. With the exponential growth of data and the necessity to process it in the quickest and most efficient way, database tools are progressing faster and faster to adapt to more challenging tasks and requests of users and businesses.

In our previous topics, we have already studied SQL relational databases and related management systems. We have also explored a new approach to processing huge amounts of data with the help of NoSQL methods, taking Mongo DB as an example. In this topic, we would like to compare RDBS and Mongo DB in order to get a clear picture of all pros and cons of the two DBMS. Finally, we'll try to figure out in which particular cases it is more efficient to choose Mongo DB rather than RDBMS and vice versa.

MongoDB main features

MongoDB is the most popular example of a document store NoSQL database. Recently the number of businesses that chose this NoSQL MS increased rapidly. So what is the secret to the success of that new type of data processing?

We remember that MongoDB is a schema-less DB that uses flexible "key-value" pairs, called documents for data storage. It means that we do not have to define a fixed structure for such a DB. In other words, its user-friendly interface fits both the developers and the administrators and is easy to use and to study. What is more, this type of DB proved itself as a highly reliable and efficient instrument in fulfilling users' needs in speed and storage. Due to its scaling out abilities, MongoDB becomes a perfect decision for rapidly developing businesses. It supports speedy iterative development helping a big number of team members cooperate easily.

MongoDB with its non-relational structure is widely used for big data processing. Other fields that benefit from MongoDB are client analytics, content management system, real-time data integration, product data management, etc.

As you can see, MongoDB has plenty of benefits. However, in order to have the full picture, let's consider its limitations as well:

  • MongoDB does not support unification, so depending on how often you want to get access to your data, you should probably update your documents on the regular basis.

  • This DB structure tends to be memory-consuming due to the "key-value" pairs, which could lead to excessive data.

  • Documents are limited to 16 MB.

  • As ACID (atomicity, consistency, isolation, durability) principles are not strictly followed in the NoSQL management system, complicated transactions could become even more complex.

Main features of RDB

After familiarizing ourselves with the basic principles of NoSQL DB, let's remember the main concepts of how RDB operates. In general most relational databases function within a relational database management system with open code, which stores data in tables, consisting of rows and columns. They use structural query language (SQL) to access and transfer data. Since its creation, RDBMS gained a vast community of users, a wide range of testing procedures, and substantial stability. Different types of RDBMS such as MySQL, Postgres, Microsoft SQL Server, etc. are often used for crucially important websites with a huge number of visitors, for e-commerce applications, for data storage applications, and for keeping logs.

However, there are certain limitations of RDB, that make the rapid spreading of NoSQL DB inevitable:

  • As far as data stored in tables, if your data is too big for any existing table, you have to alter the structure of your DB to adapt it to the existing dataset.

  • DB should be diversified within several servers, which could be complex to manage.

  • RDBMS becomes less efficient when it comes to huge databases because of scaling issues.

RDB vs MongoDB table

In order to sum up all information we previously studied, take a look at the table comparing RDB and MongoDB:

Data Model:

Mongo DB

RDB

Different models for data storage with dynamic schemas, such as "key-value", documents, graphs, wide columns, etc. Storage models are optimized for higher capacity and scalability.

This approach provides more flexibility for data upload.

Data is being normalized and transformed into tables, consisting of rows and columns. All elements, including indexes and table-relations, have a predefined schema.

Such a structure secures integrity of linked data and table relations.

Data Representation:

Mongo DB

RDB

Supports JSON with key-value matches for each record:

{
  "First_name": "Helen",
  "Last_name": "Taylor",
  "Id_card": 123,
  "Status": "Married"
}

Data is stored as a record in a row

First_name

Last_name

Id_card

Helen

Taylor

123

Data Accessibility/API:

Mongo DB

RDB

Object-oriented API allows developers to record or withdraw data structures. By using key sections of applications, users can implement search by "key-value" pairs, sets of columns or by partly structured documents, containing serial objects and application attributes.

MongoDb uses MQL(Mongo Query language) for data extraction.

Requests for data record and selection are made using SQL. Those requests are analyzed and processed by RDB.

Data selection:

db.personal_data.find()

Data selection:

SELECT * FROM personal_data;

Data insertion:

db.personal_data.insert(
{person_id:'3', 
 Last_name:'Taylor', 
 status:'Married'
})

Data insertion:

INSERT
INTO personal_data 
(First_name,
  Last_name,
  id_card,Status) 
VALUES 
('Helen',
 'Taylor',
 '123',
 'Married');

Data update:

db.personal_data.update(
{person_id::{$eq:3}},
{$set{ id_card:'135'}},
{multi:true})

Data update:

UPDATE personal_data 
SET id_card = '135' 
WHERE person_id =3;

Scalability and replication:

Mongo DB

RDB

Fits better for Horizontal scaling (scaling out). MongoDB is considered to be more scalable than conventional RDBMS. Most NoSQL systems, including MongoDB, are capable of storing your database on multiple machines at the same time while maintaining high-speed performance. This DB has in-built replication support and sharding(a method for distributing data across multiple machines).

MongoDB is a universal DB that ensures stable productivity, wherever you want to get it. At the same time, its requirements are relatively low, which allows it to run on powerful servers, virtual machines, and cloud services.

Implies Vertical scaling (scaling up).

Relational databases typically scale by increasing the computational power of the hardware or adding separate copies for read workloads.

Performance depends on the disk subsystem. Optimization of queries, indexes and table structure is usually required for maximum performance.

ACID properties:

Mongo DB

RDB

It centers around the CAP theorem (Consistency, Availability, and Partition). NoSQL databases often offer a trade-off by relaxing the strict ACID property requirements in favor of a more flexible data model that can scale out.

This makes NoSQL a great choice for high throughput, low latency use cases that need to scale out beyond a single instance.

It centers around ACID properties (Atomicity, Consistency, Isolation, and Durability).

Atomicity means that a transaction should be either fully completed or not completed at all.

Consistency means that as soon as the transaction ends, the data must conform to the database schema.

Isolation requires concurrent transactions to run separately from each other.

Durability refers to the ability to recover to the last saved state after an unexpected system failure or power outage.

When to use MongoDB or RDB

RDB is a good choice if:

  • You have just started your business and your database does not scale very much.

  • Your data structure won't change for a long time, or you have a fixed schema.

  • You have a high transaction speed.

  • Data security is your top priority.

  • You need the best support. MySQL has been around for a long time, so finding solutions to common problems is much easier.

In other words, RDB is a good choice if you are working with a legacy application that requires multiline transactions and has structured data with a clear schema.

MongoDB is a good choice if:

  • You need high data availability along with automatic and instant data recovery.

  • You are working with an unstable schema and want to reduce the cost of schema migration.

  • Your services are cloud based.

  • You want to speed up development.

MongoDB might be the right choice if you work with real-time analytics, mobile apps, IoT (Internet of Things), and more, where you can have structured or unstructured data that can grow rapidly.

Conclusion

To sum up, nowadays in the era of information, businesses are facing a new challenge of how to cope with increasing big data. Several questions below should help you choose a sufficient database structure to work with:

  • What type of data do you work with?

  • What is the amount of data you are going to analyze?

  • What resources do you possess to maintain your database?

If your data is more structured and relational, consists of small or medium data sets and you want to use it on high–traffic websites, – RDBMS could probably become the best option for you. However, if you have to collect your data from different sources, in search of more flexibility on uploading, if your data sets are huge and require unlimited space and the ability to store data on different machines is a must – you should probably turn to NoSQL for greater performance.

29 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo