# 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1615234402237/ipWjjWTRL.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233360518/GmEw2NQgS.png)


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

![Selection_480.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233473088/csRBia9ZM.png)

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

![Selection_481.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233612188/jhVz2QmnK.png)

![Selection_482.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233624932/x0V8k4D15.png)

Update
```
UPDATE crews
    -> SET ship = 'Sunny2'
    -> WHERE captain = 'Luffy';
```
![Selection_483.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233653738/dD2mP41q5.png)

Delete
```
DELETE FROM crews WHERE captain = 'Kaido';
```

![Selection_484.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233740011/AQg-skk12.png)

### MongoDB

Create a database, the database you can see in the list when you create a collection.
```
use onepiece
```
![Selection_485.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233720743/NHTJWPQx6.png)

Create a collection
```
db.createCollection('crews')
```

![Selection_486.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233773610/UQ4zEPRS2.png)

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

![Selection_487.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233798959/ZF9b_dUFW.png)

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

![Selection_488.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233879163/BtuX4V--p.png)

![Selection_489.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233904523/_yGiJBmK5.png)

Update
```
db.crews.findOneAndUpdate({"captain":"Luffy"}, { $set: {"ship":"Sunny2"} })
```
![Selection_490.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233938134/LcUQYiACJ.png)

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


![Selection_491.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1615233957057/B-guGT0O3.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!
