MongoDB Basic & Statements

 

Difference Between Terms and Syntax

SQL Terms/SyntaxMongoDB Terms/Syntax
DatabaseDatabase
TableCollection
RowDocument
ColumnField
IndexIndex
Table$lookup or embedded docs
Primary keyPrimary key
TransactionsTransactions


Contrast Between SQL and MongoDB Statements

CREATE and ALTER

SQL Schema StatementsMongoDB 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 StatementsMongoDB 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 StatementsMongoDB 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 StatementsMongoDB 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 StatementsMongoDB deleteMany() Statements
DELETE FROM employee
WHERE status = "D"
db.employee.deleteMany( { status: "D" } )
DELETE FROM employeedb.employee.deleteMany({})

Comments

Popular posts from this blog

ERR: error parsing query: found influx, expected SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL at line

How To Convert Html File/URL/String Into Image Using Python

How to get Full URL or get absolute url In Django