MongoDB Basic & Statements
Difference Between Terms and Syntax
SQL Terms/Syntax | MongoDB Terms/Syntax |
---|---|
Database | Database |
Table | Collection |
Row | Document |
Column | Field |
Index | Index |
Table | $lookup or embedded docs |
Primary key | Primary key |
Transactions | Transactions |
Contrast Between SQL and MongoDB Statements
CREATE and ALTER
SQL Schema Statements | MongoDB Schema Statements |
---|---|
CREATE TABLE employee ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) ) | db.employee.insertOne { { id: "john25", name: john, status: "A" } ) However, you can also explicitly create a collection: db.createCollection (“employee”) |
ALTER TABLE employee ADD join_date DATETIME | db.employee.updateMany( { }, {$set: {last_name: Adam}} ) |
ALTER TABLE employee DROP COLUMN join_date | db.employee.updateMany( { }, {$unset: {“Age”: “”} } ) |
INSERT
SQL INSERT Statements | MongoDB insertOne() Statements |
---|---|
INSERT INTO employee(user_id, age, status) VALUES ("test001", 45, "A") | db.employee.insertOne( { user_id: “john25”, age: 45, status: “A”} ) |
Some SELECT Queries of SQL and MongoDB
SQL SELECT Statements | MongoDB find() Statements |
---|---|
SELECT * FROM employee | db.employee.find() |
SELECT id, user_id, status FROM employee | db.employee.find( { }, {user_id: 1, status: 1} ) |
SELECT user_id, status FROM employee | db.employee.find( { }, {user_id: 1, status: 1, _id: 0} ) |
SELECT * FROM employee WHERE status = "A" | db.employee.find( { status: “A” } ) |
UPDATE Statements of SQL and MongoDB
SQL Update Statements | MongoDB updateMany() Statements |
---|---|
UPDATE employee SET status = "C" WHERE age > 25 | db.employee.updateMany( { age: { $gt: 25 } }, { $set: { status: "C" } } ) |
UPDATE employee SET age = age + 3 WHERE status = "A" | db.employee.updateMany( { status: "A" } , { $inc: { age: 3 } } ) |
Delete Records Of SQL and MongoDB
SQL Delete Statements | MongoDB deleteMany() Statements |
---|---|
DELETE FROM employee WHERE status = "D" | db.employee.deleteMany( { status: "D" } ) |
DELETE FROM employee | db.employee.deleteMany({}) |
Comments
Post a Comment