Mongodb vs Mysql - What to Choose?
Mongodb vs Mysql - What to Choose?

In this post I want to cover differences between these two most popular databases. And these are Mongodb and Mysql. And it all comes from differences between SQL and NoSQL. So, if you are interested let’s jump right into it.

What is MongoDB?

So, first of all what is Mongodb? I think this is the most popular database in the world, because it is so simple. This is exactly the same case as with Javascript. It is super easy to start, this is why it is more popular. And actually Mongodb is the perfect example of NoSQL database.

So, Mongodb stores data inside itself like documents. And actually JSON like document(s). So we have the documents with key and values and we can nest data inside.

What is MySQL?

The next most popular database is MySQL. And actually this is an SQL database, how we can get from the name. And actually we had only SQL databases until 2001 or so. And the main idea is that we are storing our data inside tables. So, we have a database, inside database we have tables, like for example users, posts, comments and so on.

Database relations

The main thing about SQL databases and MySQL in particular is that all our data are built in relational way, which actually means we have a lot of relations between different tables.

For example inside user you typically have an ID and some Name. And inside comments you typically will have a reference to the ID of this user. For example UserID.

Differences

So, here are the key differences between SQL databases and NoSQL databases. In SQL databases or relational databases we have tables. This is where we are storing data. In NoSQL databases we can have different things, for example it can be documents with JSON inside, this is most popular variant, but it can be also key-value pairs, for example like Redis or graphs.

Another important point is that relational databases have predefined schema of our data. Which actually means in every single table we are specifying what we have in this specific table. For example ID, name, surname and so on. So, our schema is fully defined and at any moment we can see the full schema of our database. In NoSQL it is completely different, it is unstructured and not defined. And this is actually why I like relational databases much more. But we will talk about it in a second.

Why NoSQL?

And as I already said in the beginning we had only SQL databases. So, now the question why people invented NoSQL databases? Are they better? And what exactly should we use?

The most important point is that NoSQL databases don't have any structure, which actually means we can change things faster. And this is extremely important in nowadays development. Nobody wants to update all these tables and change their structure and so on. But actually this is why I like SQL databases. They are super strict and super reliable. But people want to change schema fast, update things fast inside their database simply add another data without actually thinking about structure, because we need to deliver new project so fast nowadays. So in NoSQL you can add and remove things on the fly, when your requirements are changing.

The next important point is horizontal scaling versus vertical scaling. And here NoSQL databases are for sure a winner. Why is that? What does it ever mean? Traditional vertical scaling means, that you simply improve your server or you buy a bigger server. Horizontal scaling means, that you have a bunch of servers, which can be quite cheap, but you have a pool of the servers and all your data is replicated between them. And this is extremely efficient because first of all it is cheaper, it is easier to scale and all your service have something like a pool, where you can pick data from different server.

Scaling

NoSQL databases can also be much faster if we use them correctly. The main idea is that you should not use rules of relational databases inside NoSQL. Sure you can build your relations exactly the same you can have your documents with relations like between tables and it will be exactly the same. But you won’t get any benefit.

The main idea is that inside NoSQL we will duplicate data.

We will pack a lot of data together. This is why it will be much easier to get this data from the database. In relational databases we typically want to join tables. And if you have lots of data inside your tables then it will be a slow process. Inside NoSQL we can duplicate data and this is of course not that great for stability and you must be aware that you must update all your data and even your duplicates in all places.

Also from my point of view NoSQL have a huge benefit, that it is easier for beginners. You don't have SQL language inside and actually you have some simple DSL to work with databases.

Problems of NoSQL

And now I want to sum up three main problems, that I see in NoSQL databases.

  • And first of all it is data duplications. This is why I am not huge fan of NoSQL databases, because your data is not situated just in one place, which actually means you should always remember to update your data everywhere and it can be time consuming.
  • Secondly, your NoSQL database will be much bigger, because actually you duplicate data, which means it will take more place. It is not that bad nowadays, because storage is so cheap, but you must still keep it in mind.
  • And the last problem, that you will for sure have in production with NoSQL is data consistency. You will have some bugs like for example user updated a name somewhere, but it was not updated everywhere. And this is exactly the problem of data duplication.

Conclusion

So my conclusion here, if you have a small or medium application I highly recommend you to go with relational databases. Because yes, they are more difficult, but they are much stricter. You must define a schema, you must think about what you are doing, and you are always on the safe side. And while you are working with databases it is much easier to be safe than sorry. But of course if you just want to jump and try some database and if you don’t have a lot of time maybe 2 or 3 days, then I highly recommend you to look on MongoDB. Because actually you don't need to learn SQL, you don't need to understand something deeply, you can throw just a bunch of data inside, get them back and you are good to go. And also if you are interested how to deal with depression in programming, make sure to check this post also.