MongoDB vs MySQL Example

1. Introduction

These days, when it comes the time to select a database, one of the toughest decision for the developers is picking a relational or a non-relational database. While both are achievable alternatives, there are definite differences that developers must keep in mind when making a conclusion. In this tutorial, we will learn the important differences between the MongoDB and the MySQL databases.

1.1 What is MongoDB?

  • MongoDB is a high-performance NoSQL database where each dB has collections which in turn has the key-value pairs, known as documents
  • Each document has a different number of fields, size, and content. Each documentation in MongoDB is stored in a JSON-like format (i.e. Binary JSON (BSN))
  • The documents in MongoDB doesn’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent hierarchical relationships, store arrays, and other more complex structures more easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

Fig. 1: Data representation in MongoDb
Fig. 1: Data representation in MongoDB

1.2 What is MySQL?

  • It is a Relational Database Management System (RDBMS) that use the Structured Query Language (SQL),
    • To implement a database with columns, records, and indexes that provide the non-structured data
    • Offers data integrity between the rows and the tables
    • Support query caching, ACID transactions, and joins
    • Provides data access to the users by using host and query languages

Fig. 2: Data representation in MySQL
Fig. 2: Data representation in MySQL

The comparison between MongoDB and MySQL has been engaged for a while and it is important for developers to understand the differences between these two.

2. MongoDB vs. MySQL

Let’s see the fundamental differences between the relational and the non-relational databases.

  • The primary difference between these two databases is the data representation. In MongoDB, data is represented as a collection of JSON-like documents while in MySQL, the data is represented in tables, rows, and columns
  • MySQL supports the “Structured Query Language” to query a string in the database system, while MongoDB supports the object-oriented querying. Thus MongoDB offers no SQL injection attacks or parsing
  • One big picture difference is that; MySQL supports the JOIN operation to allow querying across multiple tables, while MongoDB provides multi-dimensional documents
  • MySQL supports several operations within a single atomic transaction and offers the rollback facility, while in MongoDB there is no support for transactions
  • One best thing about MongoDB is that developers are not responsible to define a schema beforehand and the records can be created on the fly. In MongoDB, any two collections need not have the same fields, while in MySQL all the rows in a table share the same columns
  • MongoDB’s performance is better than the relational databases as this NoSQL database dispense the JOIN operations and other things that can impact the performance
  • One advantage of MongoDB over MySQL is that MongoDB provides the rapid development and agile sprints i.e. NoSQL data doesn’t need to be prepped ahead of time
  • RDBMS databases offer the Atomicity, Consistency, Isolation, and Durability and this is not present in the MongoDB database
  • MongoDB allows for easier scalability through the Map Reduce feature. This means developers can get the complete functionality even if they are using the low-cost hardware
  • Relational databases offer several reporting tools to authenticate the application’s validity while there are no such reporting tools with the MongoDB
  • MongoDB offers a non-relational database model as the DB architect can easily create a database without a pulverized DB model, thereby saving the development time and cost
  • MySQL is an extremely established database offering huge community, extensive testing, and stability while MongoDB is still in development phase

2.1 How are their Queries different?

The following commands show how the queries are different in the relational and the non-relational databases.

2.1.1 Selecting records from the student table

The following code shows how this can be done in MongoDB and MySQL databases.




SELECT * from student;

2.1.2 Inserting records into the student table

The following code shows how this can be done in MongoDB and MySQL databases.


db.student.insert({ stu_id: 'stu101', stu_branch: 'Science', stu_status: 'Active' });


INSERT INTO student (stu_id, stu_branch, stu_status) VALUES ('stu101', 'Science', 'Active');

2.1.3 Updating records in the student table

The following code shows how this can be done in MongoDB and MySQL databases.


db.student.update({ stu_age: { $gt: 15 } }, { $set: { stu_branch: 'Commerce' } }, { multi: true });


UPDATE student SET stu_branch = 'Commerce' WHERE stu_age > 15;

2.2 So Which Database Is Right for Your Business?

Here’s a quick cheat sheet to get you familiar with the MySQL and MongoDB features.

Use CaseMySQL is a recommended choice for any application that has a pre-defined structure and set schemas i.e. applications that need the multi-row transactions.MongoDB offers rapid growth or the databases with no clear schema definitions. Thus, it is a good choice for content-management, internet of things, mobile applications etc.
Data StructureMySQL offers a clear and well-defined schema and supports the structured data.MongoDB doesn’t need to have a schema defined beforehand and thus the records can be created on the fly.
RiskRisk of SQL injection attacks.Less risk of attack due to modular design.
AnalysisGreat choice if the application has structured data and needs a traditional relational database.Great choice if the application has unstructured and/or structured data with the potentially rapid growth.

That’s all for this post. Happy Learning!!

3. Conclusion

So, which is the better option? The answer lies in the type of data application have, but more and more people are going the NoSQL way. I hope this article served you whatever you were looking for.


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
6 years ago

A great comparison! Thank you!

Back to top button