DEV Community

Cover image for MySQL VS MongoDB
Chirag Dave
Chirag Dave

Posted on

MySQL VS MongoDB

MongoDB and MySQL both are very useful and the differences in their basic operations and initial approach. Both are open-source and easily available.

MySQL is a open-source relational database management system that is developed and supported by Oracle Corporation. MongoDB is document oriented an open-source database developed by MongoDB, Inc. It is a popular No SQL database. We have to predefine our database schema based on the requirement in MySQL, while in MongoDB there is no need to define database schema first.

Individual records are stored as rows in a table and A table is used to store rows of similar type in MySQL. Each individual records are stored as documents In MongoDB. Documents belonging to a particular class or group as stored in a ‘collection’ In MongoDB.

We cannot change schema of the table in MySQL. We have to defined database schema first. The inputs should be entered as per the given schema. Example: In a table, if there are columns for first name, last name and there is need to enter ‘phone’ in new column in one of the entries, it will not take it as the column is not defined in schema. This cannot be achieved by MySQL but This can be achieved in MongoDB, any new field can be inserted irrespective of the schema and is thus known to have dynamic schema. The difference between the way data is stored and represented in both the databases is quite different. MongoDB stores data in the form of “JSON-like” documents and MySQL stores data in the form of “rows” of the table.

How is their query different?

=> Selecting records from the users table:
MySQL: SELECT * FROM users
MongoDB: db.users.find ()

=> Inserting records into the users table:
MySQL: INSERT INTO users (user_id, fname, lname, phone)
VALUES (‘jane’, ‘deo’, ‘7858458891’)
MongoDB: db.users.insert ({user_id :’1’, fname: ‘jane’,
lname: ‘deo’, phone: ”7858458891′ })

=> Updating records in the users table:
MySQL: UPDATE users SET fname = ‘Robert’ WHERE user_id
= 2
MongoDB: db.users.update( { user_id: { $eq: 2 } }, {
$set: { fname: ‘Robert ‘ } }, { multi: true } )

When to use MySQL and MongoDB :

MySQL and MongoDB both have their weaknesses and strengths. you should take care about your project requirements.

MySQL : If your data is structured and simple , Or if you can define your schema, if smaller volume of data then you can use MySQL. MySQL will help you to meet the goals.

MongoDB : if your data is unstructured and complex, or if you can not pre-define your schema, you have better option for MongoDB. And if you need to handle a large volume of data then   MongoDB will help you.

Pros and Cons of MongoDB and MySQL :
MySQL
Pros:

  1. It is available for free.
  2. It offers a lot of functionality even for a free database engine.
  3. It can be used to work with other databases, including DB2 and Oracle. Cons:
  4. You might have to spend a lot of time and effort to get MySQL to do the things that other systems can do automatically, e.g. creating incremental backups..
  5. Support is available in the free version, but you will need to pay for it.
  6. We need to define schema first.

MongoDB
Pros:

  1. It is fast and easy to use.
  2. Data of any structure can be accessed and stored easily and quickly
  3. We do not need to define schema first.

Cons:

  1. Setup can be a lengthy process.
  2. Some Default settings are not secure.

Top comments (1)

Collapse
 
peledzohar profile image
Zohar Peled • Edited

You're comparing apples and oranges here - Relational databases have thier place, and document databases have a different place - they're not interchangable.