MySQL vs mongoDB

Mehmet Toprak
6 min readNov 30, 2019

--

Which one is the best ?

With so much data available nowadays, it is almost impossible to manage it all without using a proper Database Management System (DBMS). Thankfully, there are many DBMS presents in the market that can be used to fulfill your business requirements. But sometimes, choosing the most appropriate database can become a frustrating task.

For many years, relational databases have seized the market, making them the optimal choice for most businesses. These include MySQL, MS SQL, Oracle, etc. They have served as a basis for a plethora of enterprise apps. But modern apps today require more scalability and diversity. As such, non-relational databases such as MongoDB have come into the picture. These are capable of dealing with massive amounts of data and are therefore replacing the current relational environment.

In this article, we will have a look at the two very popular relational and non-relational databases- MySQL and Mongo DB respectively. We will compare the two and help you understand which database better suits your business requirements. Let’s begin.

What is MongoDB

MongoDB is one of the most popular document-oriented databases under the banner of NoSQL database. It was developed from an idea in 2007 and its first version was released in 2010. It is developed and maintained by MongoDB Inc.

It employs the format of key-value pairs, here called document store. Document stores in MongoDB are created is stored in BSON files which are, in fact, a little-modified version of JSON files and hence all JS are supported. Because of this, it is frequently used for Node.js projects. Moreover, JSON facilitates the exchange of data between web apps and servers in a human-readable format. Not only that, it offers greater efficiency and reliability which in turn can meet your storage capacity and speed demands.

On top of it, the schema-free implementation of MongoDB eliminates the prerequisites of defining a fixed structure. These models allow hierarchical relationships representation and facilitate the ability to change the structure of the record.

What is MySQL

MySQL is an open-source relational database management system RDBMS. It was originally built by MySQL AB though presently owned by Oracle.

It employs the concept of storing data in rows and tables which are further classified into the database. It uses Structured Query Language SQL to access and transfer the data and commands such as “SELECT’, ‘UPDATE’, ‘INSERT’ and ‘DELETE’ to manage it.

Related information is stored in different tables but the concept of JOIN operations simplifies the process of correlating it and performing queries across multiple tables and minimize the chances of data duplication.

MongoDB vs MySQL: Comparison Chart

Flexibility of Schema: MongoDB vs MySQL

MongoDB: One of the best things about MongoDB is that there are no restrictions on schema design. You can just drop a couple of documents within a collection and it isn’t necessary to have any relation to those documents.

Though, due to the absence of joins and transactions , you need to frequently optimize your schema based on how the application will be accessing the data.

MySQL: Before you can store anything in MySQL, you need to clearly define tables and columns and also, every row in the table should have the same column. And because of this, there isn’t much space for flexibility in the manner of storing data if you follow normalization.

For example, if you run a bank, its information can be added to the table named ‘account’ as follows:

This is how MySQL stores the data. As you can see, the table design is quite rigid and it is not easily changeable. You can’t even type letters where numbers are expected.

MongoDB stores the data in the JSON type manner as described below:

Such documents can be stored in a collection as well.

Querying Language in MongoDB & MySQL

MongoDB: This uses un-Structured Query Language. To build a query in JSON documents, you need to specify a document with properties you wish the results to match.

It is typically executed using a very rich set of operators, linked with each other using JSON. MongoDB treats each property as having an implicit boolean AND. It natively supports boolean OR queries, but you must use a special operator ($or) to achieve it.

MySQL: This uses the Structured Query Language SQL to communicate with the database. You can use the following commands to query the data in MySQL database- ‘SELECT’, ‘UPDATE’, ‘INSERT’ and ‘DELETE’.

Let’s have a quick comparison.

When to use MongoDB?

If the following are your requirements, you should be using MongoDB:

  • When you need high availability of data with automatic, fast and instant data recovery.
  • If you have an unstable schema and you want to reduce your schema migration cost.
  • If you don’t have a Database Administrator (but you’ll have to hire one if you’re going to go BIG).
  • If most of your services are cloud-based, MongoDB is best suitable for you, as its native scale-out architecture enabled by ‘sharding’ aligns well with horizontal scaling and agility offered by cloud computing.

MongoDB: Pros & Cons

  • MongoDB is best when you want the flexibility of schema. You can easily use replica sets with MongoDB and can take advantage of scalability. Expansion plans are flexible and can be easily achieved by adding more machines and RAM to the system. It also includes document validations and integrated systems.
  • The cons of MongoDB include higher data size over the period of time. Due to the lack of atomic transactions, the speed is comparatively low compared to NoSQL. Also, the solution is quite infant and hence cannot replace the legacy systems directly.

When to use MySQL?

If the following are your requirements, you should be using MySQL:

  • If you’re just starting and your database is not going to scale much, MySQL will help you in easy and low-maintenance setup.
  • If you’ve fixed schema and a data structure aren’t going to change over the time .
  • If you want high performance on a limited budget.
  • If high transaction rate is going to be your requirement (like BBC around 30,000 inserts/minute, 4000 selects/hour)
  • If data security is your top priority, MySQL is the most secure DBMS.

MySQL: Pros & Cons

  • MySQL is around the block for a long time. One of the main pros is that it’s community driven. Being a mature solution, it supports JOIN, atomic transactions with privilege and password security system.
  • With MySQL, you may end devoting a lot of time and efforts which other platforms might do automatically for you, like incremental backups. The main issue with MySQL is scalability. No inbuilt XML and OLAP.

Conclusion

To answer the main question on “Why I should use X over Y?” you need to take into consideration your project goals and many other relevant things.

MySQL is highly-organized for its flexibility, high performance, reliable data protection and ease in management of data. Proper data indexing can resolve your issue with performance, facilitate interaction and ensure robustness.

But if your data is not structured and complex to handle, or pre-defining your schema is not coming off easy for you, you should better opt for MongoDB. What’s more, if it is required to handle a large volume of data and store it as documents- MongoDB will help you a lot!

--

--

Mehmet Toprak

Data Scientist | Machine Learning Engineer | Data Engineer