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

q6_embeed

The document provides a MongoDB script for managing a database of departments and their employees, including insertion of department data and various queries. It covers finding employees by department, updating salaries, adding new employees, deleting employees, and counting total employees in a specific department. The script demonstrates operations such as filtering by salary and gender, as well as aggregating data.

Uploaded by

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

q6_embeed

The document provides a MongoDB script for managing a database of departments and their employees, including insertion of department data and various queries. It covers finding employees by department, updating salaries, adding new employees, deleting employees, and counting total employees in a specific department. The script demonstrates operations such as filtering by salary and gender, as well as aggregating data.

Uploaded by

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

db.departments.

insertMany([
{
"deptid": 1,
"deptname": "IT",
"employees": [
{ "eid": 101, "ename": "John Doe", "salary": 80000, "gender": "Male" },
{ "eid": 102, "ename": "Jane Smith", "salary": 90000, "gender": "Female" }
]
},
{
"deptid": 2,
"deptname": "HR",
"employees": [
{ "eid": 103, "ename": "Raj Kumar", "salary": 60000, "gender": "Male" },
{ "eid": 104, "ename": "Priya Gupta", "salary": 65000, "gender": "Female" }
]
},
{
"deptid": 3,
"deptname": "Finance",
"employees": [
{ "eid": 105, "ename": "Amit Sharma", "salary": 70000, "gender": "Male" },
{ "eid": 106, "ename": "Sanya Desai", "salary": 75000, "gender": "Female" }
]
}
])

1. Find all departments and their employees


db.departments.find()

2. Find all employees in the IT department


db.departments.find({ deptname: "IT" }, { employees: 1, _id: 0 })

3. Find employees in the Finance department with a salary greater than 70000
db.departments.find(
{ deptname: "Finance", "employees.salary": { $gt: 70000 } },
{ "employees.$": 1, _id: 0 }
)

4. Find all male employees across all departments


db.departments.find(
{ "employees.gender": "Male" },
{ "employees.$": 1, _id: 0 }
)

5. Find the department with the highest salary employee

6. Update the salary of 'John Doe' in IT department to 85000


db.departments.updateOne(
{ "deptname": "IT", "employees.ename": "John Doe" },
{ $set: { "employees.$.salary": 85000 } }
)

7. Add a new employee to the HR department (Rajeev, salary 70000, male)


db.departments.updateOne(
{ deptname: "HR" },
{ $push: { employees: { eid: 107, ename: "Rajeev", salary: 70000, gender:
"Male" } } }
)

8. Delete an employee from the Finance department


db.departments.updateOne(
{ deptname: "Finance" },
{ $pull: { employees: { ename: "Amit Sharma" } } }
)

9. Find departments where employees have a salary greater than 70000


db.departments.find(
{ "employees.salary": { $gt: 70000 } },
{ deptname: 1, employees: 1, _id: 0 }
)

10. Find the total number of employees in the IT department


db.departments.aggregate([
{ $match: { deptname: "IT" } },
{ $unwind: "$employees" },
{ $count: "totalEmployees" }
])

You might also like