0% found this document useful (0 votes)
4 views24 pages

Practical 3 Solution

The document outlines basic CRUD operations using MongoDB, including creating databases for students and employees, and defining their respective collections. It provides queries for inserting, displaying, updating, and deleting records in these collections, along with specific examples for student and employee data. The document serves as a practical guide for performing fundamental database operations in MongoDB.

Uploaded by

pathakpriya2206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Practical 3 Solution

The document outlines basic CRUD operations using MongoDB, including creating databases for students and employees, and defining their respective collections. It provides queries for inserting, displaying, updating, and deleting records in these collections, along with specific examples for student and employee data. The document serves as a practical guide for performing fundamental database operations in MongoDB.

Uploaded by

pathakpriya2206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Practical3

Basic CRUD Operations with MongoDB

1. Create Database named as “student” with fields like rollno, name,


semester, birthdate, per.

>use student

2. Create Database named as“employee”with fields like


emp_id,name,department,joiningdate,city,contact_no,e-mail,salary.

>use EmployeeDB

switchedtodbEmployeeDB
3. Create Student_Info Collection in student.

Query :
>db.createCollection('student_Info')

ScreenShot:

4. Create Emp_Info Collection in employee.

Query :

>db.createCollection('employee_Inf

o')ScreenShot:
5. Insert 10 document in both Collection.

StudentDB:

Query:

>db.student_Info.insertMany([

...{rollno:39,name:"Rohit",semester:2,birthdate:ISODate("2000-05-15"),
per:85.5},

...{rollno:47,name:"Meet",semester:3,birthdate:ISODate("1999-10-20"),
per:78.3},

...{ rollno:37,name: "Divya",semester:1, birthdate:ISODate("2001-03-08"),


per:91.7},

...{rollno:48,name: "Dhruv",semester:4,birthdate:ISODate("1998-12-11"),
per:79.9},

...{rollno:43,name:"Janki",semester:2,birthdate:ISODate("2000-08-25"),
per:88.2},

...{ rollno:26,name: "Kunal",semester:3, birthdate: ISODate("1999-04-30"),


per:82.6},

…{rollno:22,name:"Bhumika",semester:2,birthdate:ISODate("2000-11-
05"),per:76.4},

...{rollno:50,name:"Nakshatri",semester:1,birthdate:ISODate("2001-07-
18"),per:94.1},
...{rollno:68,name:"Prince",semester:4,birthdate:ISODate("1998-09-14"),
per:75.8},

...{rollno:2,name:"Dixita",semester:3,birthdate:ISODate("1999-01-22"),
per:89.5}

... ])

ScreenShot
:
EmployeeDB:

Query:
>db.employee_Info.insertMany([
{ emp_id: 101, name: "Rohit", department: "HR", joining_date:
ISODate("2023-01-15"), city: "New York", contact_no: "123-456-7890", email:
"rohit@example.com", salary:60000 },
{emp_id:102,name:"Meet",department:"Marketing",joining_date:ISODate("
2022-11-20"),city:"LosAngeles",contact_no:"456-789-
0123",email:"meet@example.com",salary:65000 },
{emp_id:103,name:"Divya",department:"Finance",joining_date:ISODate("2
023-03-05"),city:"Chicago",contact_no:"789-012-
3456",email:"divya@example.com",salary:70000 },
{emp_id:104,name:"Dhruv",department:"Engineering",joining_date:ISODat
e("2022-09-10"),city:"SanFrancisco",contact_no:"012-345-
6789",email:"dhruv@example.com",salary:75000 },
{emp_id:105,name:"Janki",department:"Operations",joining_date:ISODate(
"2023-05-15"),city:"Houston",contact_no:"234-567-
8901",email:"janki@example.com",salary:80000 },
{ emp_id: 106, name: "Kunal", department: "IT", joining_date:
ISODate("2022-07-20"),city:"Austin",contact_no:"567-890-
1234",email:"kunal@example.com",salary:85000 },
{emp_id:107,name:"Bhumika",department:"Sales",joining_date:ISODate("
2023-02-25"),city:"Seattle",contact_no:"890-123-
4567",email:"bhumika@example.com",salary:90000 },
{emp_id:108,name:"Nakshatri",department:"HR",joining_date:ISODate("20
22-12-30"),city:"Boston",contact_no:"345-678-
9012",email:"nakshatri@example.com",salary:95000 },
{emp_id:109,name:"Prince",department:"Marketing",joining_date:ISODate(
"2023-06-05"),city:"Miami",contact_no:"678-901-
2345",email:"prince@example.com",salary:100000 },
{emp_id:110,name:"Dixita",department:"Finance",joining_date:ISODate("2
022-08-10"),city:"Dallas",contact_no:"901-234-
5678",email:"dixita@example.com",salary:105000 }
])
ScreenShot:
6. Display first five documents from student collection.

Query:
1. >db.student_Info.find().limit(5)//ForNormal Output

2. >db.student_Info.find().limit(5).pretty()//ForPrettyOutput

ScreenShot:
7. Displayfirst7documentsfromemployeebyusingprettyfunction.

Query :

>db.employee_Info.find().limit(7).pretty()

ScreenShot:
8. Display top 3 students who got maximum percentage.

Query :

>db.student_Info.find().sort({per:-1}).limit(3).pretty()

ScreenShot:
9. Displaytopthreehighestsalarypaid employee.

Query:

>db.employee_Info.find().sort({salary: -1}).limit(3).pretty()

ScreenShot:
10. DisplaybirthdateofRohitfromstudentcollection.

Query :

> db.student_Info.find({ name: "Rohit" }, { _id: 0, birthdate:

1 })ScreenShot:
11. Displaysalaryof Rohitfromsalary collection.

Query :

>db.employee_Info.find({name:"Rohit”})

ScreenShot:
12. Displaythoseemployee whosesalary lessthan 80000.

Query :

>db.employee_Info.find({salary: {$lt:80000}}).pretty()

ScreenShot:
13. Displaythosestudentlistwhosepercentagegreaterthan50.

Query :

>db.student_Info.find({ per: {$gt: 50}}).pretty()

ScreenShot:
14. UpdatedepartmentofRohitfromHR toadmin.

Query
: db.employee_Info.updateOne(
…{name:"Rohit"},//Filter:Finddocumentwithname"Rohit"
…{$set:{department:"admin"}}//Update:Setdepartmentto"admin"
…)

Screenshot:

BeforeUpdate:

AfterUpdate:
15. Updatedepartmentofrajeshfromaccount toadmin.

Query :
>db.student_Info.updateMany({},{$set:{semester:6}})

ScreenShot:
BeforeUpdate :

AfterUpdate:
16. Updatesalary ofRohit from50000 to60000.

Query
:
>db.employee_Info.updateOne({name:"Rohit"},{$set:{salary:60000}})

ScreenSho
t: BeforeUpdate:

AfterUpdate:
17. Increasesalary ofRohitby1000 rs.

Query :

db.employee_Info.updateOne(
{name:"Rohit"},//Filter:Finddocument withname"Rohit"
{$inc:{salary: 1000}}//Update:Incrementsalary by1000
)

ScreenSho
t: BeforeUpdate:

AfterUpdate:
18. DeletethedocumentofDixitafrom studentcollection.

Query :
>db.student.deleteOne({name:"Dixita"})

Screenshot:

BeforeDelete:

AfterDelete:
19.Deletedocumentsofemployeewhosesalarylessthan12000.

Query:
>db.employee_Info.deleteMany({ salary: { $lt:12000}})

Screenshot:

BeforeDelete:

AfterDelete:

EnrollmentNo:219830307047 21
20. Deletethedocumentofemployee wholivesinRajkot.

Query :

>db.employee_Info.deleteMany({ city: "Rajkot"

ScreenShot
: })Before Delete:

AfterDelete:

EnrollmentNo:219830307047 22
21. Deletedocumentsof employeewhosesalary between25000 to 30000.

Query :

>db.employee_Info.deleteMany({salary: { $gte:25000,$lte:30000}})

Screenshot:

BeforeDelete:

EnrollmentNo:219830307047 23
AfterDelete:

EnrollmentNo:219830307047 24

You might also like