Play this article
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;
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
);
Insert
INSERT INTO crews (name, ship, captain) VALUES
-> ('Straw Hat Pirates','Sunny','Luffy');
Find
SELECT * FROM crews;
SELECT * FROM crews WHERE captain = 'Luffy';
Update
UPDATE crews
-> SET ship = 'Sunny2'
-> WHERE captain = 'Luffy';
Delete
DELETE FROM crews WHERE captain = 'Kaido';
MongoDB
Create a database, the database you can see in the list when you create a collection.
use onepiece
Create a collection
db.createCollection('crews')
Insert
db.crews.insert(
{
"name":"Straw Hat Pirates",
"ship":"Sunny",
"captain":"Luffy"
}
)
Find
db.crews.find()
db.crews.find({"captain":"Luffy"})
Update
db.crews.findOneAndUpdate({"captain":"Luffy"}, { $set: {"ship":"Sunny2"} })
Delete
db.crews.findOneAndDelete({"captain":"Kaido"})
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!