MySQL is one of the most popular database systems, and the one that almost everyone learns first. So, I decided to compare MongoDB with MySQL. In this article, I’ll cover
(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)
MySQL is a cross-platform database system written in C and C++ and maintained by Oracle Corporation. It’s a relational database system based on SQL (Structured Query Language). MySQL was first released in 1995 and has come a long way, attracting a large user base. By now, it’s a very stable product. The latest version, 8.0, was released in April 2020.
In MySQL, you store data in tables, composed of rows and columns. That means there is a predefined schema for databases. Here are a few more key features of MySQL:
MongoDB is a cross-platform database system written in C++ and maintained by Mongo Inc. MongoDB was released in 2009, targeting the most modern data handling demands of software applications. The product gets better with each release, allowing developers to make the most of it—and attracting more users every day.
Unlike MySQL, MongoDB a document-oriented NoSQL database system that consists of collections and documents. MongoDB is a schema-less database system, which means that documents in the same collection can have different structures. Here are a few more key features of MongoDB.
These are some well-known companies and organizations use MySQL or MongoDB, illustrating the popularity of both. (I have a hunch many companies use both.)
MySQL | MongoDB |
---|---|
Airbnb Sony NASAU.S. Navy Netflix YouTube CERN Pearson |
Nokia Adobe eBay EA Sports Cisco Verizon PayPal Pearson |
MySQL stores data in tabular structures called tables. Instead of tables, MongoDB has collections.
Here is a row in a MySQL table, which is called a record:
name | university | age | address |
John | UCSC | 23 | 766/A, ABC Street, are DEF City |
A row in a MySQL table
In MongoDB, each record is stored as a JSON-like document:
A document in a MongoDB collection
Different documents in a single collection can have different structures, while every record in a MySQL table should have the same structure.
Now let’s have a look at how queries are used in both databases. In each one, let’s see how to do some basic operations:
You might notice that MongoDB queries are much more familiar to developers as they are written in JavaScript.
SELECT * FROM employee
db.employee.find()
INSERT INTO employee (emp_id, emp_name, role) VALUES ('emp001', 'Mike', 'Admin')
db.employee.insert({ emp_id: 'emp001', name: 'mike', role: 'admin' })
UPDATE employee SET role = 'manager' WHERE name = 'Mike'
db.employee.update( {"name" : "mike"}, {$set: { "role" : "manager"}} );
We already know that there are two types of database systems:
The differences between those database types are rooted in: how they are designed, how they store data, and the data types they support. To maximize your benefit, always select the most suitable database according to the goals and requirements of your project.
Now let’s discuss scenarios in which it is more convenient to use MongoDB over MySQL.
There’s one more reason you might choose MongoDB over MySQL. A general distinction between these two databases is that MongoDB is much more developer-friendly than MySQL. Therefore, if you are not able or willing to hire a database administrator (DBA), you might want to use MongoDB for your application.
Here are some common issues with MySQL:
Here are some common disadvantages of MongoDB:
Database performance can differ depending on:
For example, if you develop a system that processes a huge amount of transactions, like an accounting system, you should use MySQL. However, if you are going to build a real-time analytics system, MongoDB would be better. Therefore, I always recommend deciding which database management system to use based on the type of your software application.
For related reading, explore these resources: