Database war
Mysql & Mongodb
Queries

Database war Mysql & Mongodb Queries

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

This time I'm going to talk about how to do Queries

Mysql

Create a database

CREATE DATABASE IF NOT EXISTS onepiece;

Selection_479.png

Create a table

CREATE TABLE crews (
     id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
     name VARCHAR(50) NOT NULL,
     ship VARCHAR(50) NOT NULL,
     captain VARCHAR(50) NOT NULL
     );

Selection_478.png

Insert

INSERT INTO crews (name, ship, captain) VALUES
    -> ('Straw Hat Pirates','Sunny','Luffy');

Selection_480.png

Find

SELECT * FROM crews;
SELECT * FROM crews WHERE captain = 'Luffy';

Selection_481.png

Selection_482.png

Update

UPDATE crews
    -> SET ship = 'Sunny2'
    -> WHERE captain = 'Luffy';

Selection_483.png

Delete

DELETE FROM crews WHERE captain = 'Kaido';

Selection_484.png

MongoDB

Create a database, the database you can see in the list when you create a collection.

use onepiece

Selection_485.png

Create a collection

db.createCollection('crews')

Selection_486.png

Insert

 db.crews.insert(
    {
        "name":"Straw Hat Pirates",
        "ship":"Sunny",
        "captain":"Luffy"
    }
 )

Selection_487.png

Find

db.crews.find()
db.crews.find({"captain":"Luffy"})

Selection_488.png

Selection_489.png

Update

db.crews.findOneAndUpdate({"captain":"Luffy"}, { $set: {"ship":"Sunny2"} })

Selection_490.png

Delete

db.crews.findOneAndDelete({"captain":"Kaido"})

Selection_491.png

Conclusion

  • Structure: Mysql is necessary to give a name to each field, but MongoDB is not necessary it's enough.
  • Mysql has tables and MongoDB has collections.
  • Mysql has rows and columns and MongoDB has documents.

Would you like to learn about advances queries in databases?

I hope you enjoy my post and remember that I am just a Dev like you!