0% found this document useful (0 votes)
350 views

Mongo DB Basic Commands

The document discusses queries in MongoDB for a teacher database, including: 1) Creating a teacher collection with fields like teacher ID, name, department, salary, and status. 2) Comparing SQL commands to MongoDB commands for CRUD operations, indexing, aggregation, and other functions. 3) Examples of inserting, finding, updating, sorting, counting, and deleting documents in MongoDB collections.

Uploaded by

gauri
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
350 views

Mongo DB Basic Commands

The document discusses queries in MongoDB for a teacher database, including: 1) Creating a teacher collection with fields like teacher ID, name, department, salary, and status. 2) Comparing SQL commands to MongoDB commands for CRUD operations, indexing, aggregation, and other functions. 3) Examples of inserting, finding, updating, sorting, counting, and deleting documents in MongoDB collections.

Uploaded by

gauri
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Queries in MongoDB

Teacher Database.
 we are creating Teacher Database. Which contain
the information of Teacher_id, name of a teacher,
department of a teacher, salary and status of a
teacher. Here status is whether teacher is approved
by the university or not.

 Our main aim is to implement all the DDL & DML


queries on the Teacher Database and difference
between SQL Commands and mongodb commands.
SQL Vs MongoDB
SQL Concepts MongoDB Concepts

Database Database
Table Collection
Row Document 0r BSON Document
Column Field
Index Index
Table Join Embedded Documents & Linking
Primary Key Primary Key
Specify Any Unique Column Or In Mongodb, The Primary Key Is
Column Combination As Primary Key. Automatically Set To The _Id Field.

Aggregation (E.G. Group By) Aggregation Pipeline


Executables
Oracle MySQL MongoDB

Database Server oracle mysqld mongod

sqlplus mongo
Database Client mysql
Table Creation in Oracle
CREATE TABLE Teacher_Info( Teacher_id Varchar(10), Teacher_Name
Varchar(10),
Dept_Name Varchar(10), Salary Number(10), Status char(1), PRIMARY KEY (id) );
SQL:
INSERT INTO TeacherInfo(Teacher_id, Teacher_Name, Dept_Name, Salary, Status)
VALUES (“Pic001", “Ravi”, “IT”,30000, “A");
INSERT INTO TeacherInfo(Teacher_id, Teacher_Name, Dept_Name, College_Name,
Status) VALUES (“Pic002", “Mangesh”, “IT”,20000, “A");
INSERT INTO TeacherInfo(Teacher_id, Teacher_Name, Dept_Name, Salary,
Status) VALUES (“Pic003", “Akshay”, “Comp”,25000, “N");
o/p
Teacher_Info
Teacher_id Teacher_Name Dept_Name Salary Status

Pic001 Ravi IT 30000 A


Pic002 Mangesh IT 20000 A
Pic003 Akshay Comp 25000 N
DDL,DML Statements in Oracle
 DDL:
 Create
 Alter
 Drop
 DML:
 Insert
 Delete
 Update
 Drop
MongoDB: Creation of Document
{
Teacher_id: “Pic001",
Teacher_Name: “Ravi”,
Dept_Name: “IT”,
Sal: 30000,
status: 'A'
}
OR
db.createCollection(“Teacher_info")
Insert Command:
db.Teacher_info.insert( { Teacher_id: “Pic001", Teacher_Name:
“Ravi",Dept_Name: “IT”, Sal:30000, status: "A" } )
db.Teacher_info.insert( { Teacher_id: “Pic002", Teacher_Name:
“Ravi",Dept_Name: “IT”, Sal:20000, status: "A" } )
db.Teacher_info.insert( { Teacher_id: “Pic003", Teacher_Name:
“Akshay",Dept_Name: “Comp”, Sal:25000, status: “N" } )
Retrieving data from Mongodb:
 > db.Teacher_info.find()
 { "_id" : ObjectId("53a2d8ac8404f005f1acc666"), "Teacher_id" :
"pic001", "Teache
 r_name" : "Ravi", "Dept_name" : "IT", "sal" : 20000, "status" : "A" }
 { "_id" : ObjectId("53a2d8fc8404f005f1acc667"), "Teacher_id" :
"pic001", "Teache
 r_name" : "Ravi", "Dept_name" : "IT", "sal" : 20000, "status" : "A" }
 { "_id" : ObjectId("53a2d91b8404f005f1acc668"), "Teacher_id" :
"pic003", "Teache
 r_name" : "Akshay", "Dept_name" : "IT", "sal" : 25000, "status" : "N"
}
 { "_id" : ObjectId("53a2da038404f005f1acc669"), "Teacher_id" :
"pic003", "Teache
 r_name" : "Akshay", "Dept_name" : "IT", "sal" : 25000, "status" : "N"
}
 >
SQL & Mongodb Commands
SQL SELECT Statements MongoDB find() Statements

SELECT * FROM Teacher_info; db.Teacher_info.find()

SELECT * FROM Teacher_info WHERE sal db.Teacher_info.find( {sal: 25000})


= 25000;

SELECT Teacher_id FROM Teacher_info db.Teacher_info.find( {Teacher_id:


WHERE Teacher_id = 1; "pic001"})
SQL & Mongodb Commands
SELECT * FROM Teacher_info db.Teacher_info.find({status:{$ne:
WHERE status != "A“; "A"}})

SELECT * FROM Teacher_info db.Teacher_info.find({status:"A",


WHERE status = "A" AND sal = sal:20000})
20000;

SELECT * FROM Teacher_info > db.Teacher_info.find( { $or: [ {


WHERE status = "A" OR sal = 50000; status: "A" } , { sal:50000 } ] } )

SELECT * FROM Teacher_info db. Teacher_info.find( { sal: { $gt: 40000


WHERE sal > 40000 }})

SELECT * FROM Teacher_infoWHERE db. Teacher_info.find( { sal: { $gt: 30000


sal < 30000 }})
SQL & Mongodb Commands
SELECT * FROM Teacher_info db. Teacher_info.find( { status: "A"
WHERE status = "A" ORDER BY } ).sort( { sal: 1 } )
SAL ASC
SELECT * FROM users WHERE status db. Teacher_info.find( { status: "A" }
= "A" ORDER BY SAL DESC ).sort( {sal: -1 } )

SELECT COUNT(*) FROM db. Teacher_info.count()


Teacher_info; or
db. Teacher_info.find().count()

SELECT DISTINCT(Dept_name) db. Teacher_info.distinct(


FROM Teacher_info; “Dept_name" )
Update Records

UPDATE Teacher_info SET db. Teacher_info.update( { sal: { $gt:


Dept_name = “ETC" WHERE sal > 25000 } }, { $set: { Dept_name: “ETC"
250000 } }, { multi: true } )

UPDATE Teacher_infoSET sal = sal + db. Teacher_info.update( { status: "A" }


10000 WHERE status = "A" , { $inc: { sal: 10000 } }, { multi: true } )
Delete Records

DELETE FROM Teacher_info WHERE db.Teacher_info.remove({Teacher_id:


Teacher_id = “pic001" "pic001"});

DELETE FROM Teacher_info; db.Teacher_info.remove({})


Alter Table in Oracle & MongoDb
Oracle:
ALTER TABLE Teacher_info ADD join_date
DATETIME

MongoDb:
At the document level, update() operations
can add fields to existing documents using
the $set operator.
Ex:
db.Teacher_info.update( { }, { $set: { join_date:
new Date() } }, { multi: true} )
Drop Command:
Oracle:
DROP TABLE Teacher_info

Mongo:
db.Teacher_info.drop()

You might also like