Skip to content

MongoDB VS MySQL Explained AQSA Spark By {Examples}

  • by

What are the similarities and differences between MongoDB vs MySQL? MongoDB is well known as a NoSQL database where the data are kept in collections of documents. It facilitates complicated queries and has a query language based on JSON. For challenging data processing operations, MongoDB makes use of the aggregation framework.

On the other hand, MySQL is a database with a relational structure that employs tables with schemas to store data. MySQL serves as a query language, which is a powerful and widely used language for querying and managing relational databases. Let’s check out the comparison of MongoDB and MySQL in detail.

1. MongoDB vs MySQL Performance

MongoDB generally has faster read and write performance for unstructured or semi-structured data. MySQL can also perform good reading and writing with proper indexing and query optimization.

MongoDB can grow horizontally by splitting data amongst numerous servers, making managing massive data simpler. While MySQL can extend horizontally as well, managing and setting it up takes more work.

MySQL is known for its strong consistency guarantees and ACID compliance. On the other side, MongoDB trades off some consistency for scalability and performance advantages. MongoDB typically requires more memory and storage than MySQL due to its document-based storage model.

2. MongoDB Queries Vs. MySQL Queries

As we know, MongoDB and MySQL are both widely used databases, but they have significant differences in their query language and functionality. Below, we will perform the queries of both these databases to distinguish them.

2.1 CREATE query

MongoDB requires no CREATE query to create the document. While MySQL needed the CREATE query to create the table. Here, we have given a structure of the MYSQL CREATE query, which creates the table student along with the columns.

# MYSQL CREATE query
CREATE TABLE student
(id INT PRIMARY KEY,
name VARCHAR(20),
age INT ,
gender CHAR(10),
course VARCHAR(40));

2.2 INSERT query

Next is the INSERT query, which is supported by both databases. In MongoDB, using either the insertOne() or insertMany() methods, we can add a new record to the collection. Here, for the following documents to be added to the collection, we deployed the insertMany() method.

# Usage of insertMany() in MongoDB
db.student.insertMany([
{
_id: 1,
name: “Jimmy”,
age: 24,
gender: “Male”,
course: “Python”

},
{
_id: 2,
name: “Elena”,
age: 23,
gender: “Female”,
course: “Python”
},
{
_id: 3,
name: “Caroline”,
age: 25,
gender: “Female”,
course: “Python”
}
]
)

However, to create new rows, MySQL employs the INSERT INTO statement, which is followed by the name of the table and the values to be entered. The statement of INSERT INTO given below adds three rows to the given table.

# MySQL INSERT INTO statement
INSERT INTO student(id, name, age, gender, course)
VALUES(1, “Elena”, 23, “Female”, “Python”),
(2, “Alice”, 31, “Female”, “Python”),
(3, “Sam”, 20, “Male”, “Python”);

2.3 FETCH query

The fetch queries for MongoDB and MySQL are significantly different from each other. The find() method is deployed to fetch the documents from the collection. The query of find() method called with the collection name retrieved all the documents.

# MongoDB Find method
db.student.find()

These are all the documents fetched from the collection of MongoDB.

Conversely, the SELECT statement retrieves rows in the MySQL database. We have used the SELECT statement below with the asterisk symbol * to fetch all the records of the collection.

# SELECT statement in MySQL
SELECT *FROM student;

Here, it fetched all the rows of the table inserted above.

2.4 UPDATE query

Then comes the UPDATE query for MongoDB and MySQL, which has the same concept but a different structure. We use the updateOne() or updateMany() method to modify the data of the documents that match a specified set of criteria. Here, we performed the updateMany() method query, which updates all the documents with the specified value below.

# updateMany() method in MongoDB
db.student.updateMany({}, {$set:{course:”MongoDB”}})

 All the fields course in the documents of the collection are modified with the value MongoDB.

More details about the MongoDB update function can be found here. Also, in case you want to update multiple documents in MongoDB, you can use the following link.

On the other hand, the update operation of MySQL requires an UPDATE statement to modify one or many rows that match a specified set of criteria. The given query of UPDATE only updates the specific row of the table.

# update operation of MySQL
UPDATE student SET name = “Anna” WHERE id = 2;

The table is now updated with the name Anna where the row _id is 2.

2.5 DELETE query

Similarly, both databases support the DELETE query but in a different manner. MongoDB called the delete methods, which include deleteOne() or deleteMany() to remove one or many documents that match a specified condition. There, we provide the deleteOne() method query to remove only one document.

# Usage of deleteOne() method
db.student.deleteOne({name: “Caroline”})

The matched document is now removed from the collection, as shown. If we use the deleteMany() method instead of the deleteOne() method, all the documents will be removed. 

On the contrary, MySQL uses the DELETE statement to remove the rows. The DELETE statement is performed below to remove the specific row. The DELETE statement of MYSQL is performed like this.

# DELETE statement of MYSQL
DELETE FROM student WHERE age = 31;

Now, the table’s provided row has been removed.

3. Conclusion

In conclusion, we have seen the comparison of both the MongoDB and MySQL databases with their performances and structure. However, based on the specific implementation requirements, a database is preferred.

 What are the similarities and differences between MongoDB vs MySQL? MongoDB is well known as a NoSQL database where the data are kept in collections of documents. It facilitates complicated queries and has a query language based on JSON. For challenging data processing operations, MongoDB makes use of the aggregation framework. On the other hand,  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *