Which one to choose MongoDB or MySQL??

Which one to choose MongoDB or MySQL??

Hey everyone, nice to meet you again. If you wonder which one is better MongoDB or MySQL database, then you are at right place to know about it. I hope you will enjoy this another journey of learning also, so let's start the journey.

image.png

What are SQL type Databases?

Stores data in relation/table format that is in form of rows and columns

What are NoSQL Databases?

Stores data in form of JSON-like documents

but wait.. is MongoDB stores data in form of JSON format??

The answer is NO. Yes you heard right. MongoDB database stores data in form of BSON format and not in JSON format.

So, what's the difference between BSON and JSON?

BSON stands for Binary JSON.

For example, below one is the example of JSON format

{
    "name" : "Anubhav"
}

and the conversion of above JSON to BSON is

\x16\x00\x00\x00             // Size of the Document
\x02                         // 0x02 = type String
name\x00                    // field name
\x06\x00\x00\x00Anubhav\x00    // field value        
\x00                         // Used to represent end of object

so, BSON is binary file format that is used to store serialized JSON documents in a binary-encoded format

Why BSON??

  • can be parsed easily and very quickly
  • BSON objects are designed to be highly traversable and lightweight in nature
  • offers a wide range of data types, like – date type, etc.), many of which are not supported by JSON
  • takes up less space and offers faster scan speed as compared to JSON objects

but there is some disadvantage of BSON format like BSON supports fast traversal of the BSON document. In order to support it, BSON adds extra information (like the length of sub objects, etc.) to the document. In some cases, this leads to increase in the size of the document and decreases the efficiency when compared to JSON.

Database like AnyDB, redis etc stores information into JSON format while MongoDB stores data in BSON format.

so till now, we have understood what is BSON like format and why MongoDB stores data in BSON format. Now coming back to the question again, which one is better MongoDB or MySQL database?? So let's explore further...

image.png

Advantages of SQL type Databases

  • highly organized
  • handles complex operations well
  • respects ACID properties
  • Long-term stability

but there are some Limitations as well of SQL type databases

  • more complex
  • not highly scalable
  • developers need to create a schema that will remain consistent for the entire database during the whole lifecycle of the application. Switching to a different schema is complicated and time-consuming, which requires developers to invest a lot of time and effort in planning and research.

Advantages of NoSQL type Databases

  • highly and easily scalable
  • maintaining NoSQL Servers is Less Expensive
  • No Schema or Fixed Data model
  • Support Integrated Caching

but of course some disadvantages too are there...

  • no stored procedures
  • no fixed standards as schema is not fixed

image.png

so, the question is which one to choose among these two....

let's answer this

  1. if you have limited budget and want high performance, use SQL type databases
  2. if you have fixed schema that is not going to change over the time, then again use SQL type databases
  3. if you need high availability of data, then use NoSQL type databases

So the summary is, it's our choice which one to choose among them because both the type of databases are good and it depends on our requirement which one to prefer.

I hope you loved this article and learned something new. Thanks for reading..

image.png

Did you find this article valuable?

Support Anubhav Goyal's Blog by becoming a sponsor. Any amount is appreciated!