0% found this document useful (0 votes)
11 views3 pages

q3 Mongo Emp

The document contains a series of MongoDB commands for managing a collection of employee records, including inserting, querying, updating, and deleting employee data. It includes operations to filter employees based on experience, salary, job title, and working hours, as well as commands to sort and aggregate data. Additionally, it demonstrates how to create views and perform updates on multiple records based on specific conditions.

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)
11 views3 pages

q3 Mongo Emp

The document contains a series of MongoDB commands for managing a collection of employee records, including inserting, querying, updating, and deleting employee data. It includes operations to filter employees based on experience, salary, job title, and working hours, as well as commands to sort and aggregate data. Additionally, it demonstrates how to create views and perform updates on multiple records based on specific conditions.

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/ 3

db.myemployees.

insertMany([
{ empid: 1, ename: "Ajay", exp: 12, salary: 75000, hours: 5, jobtitle:
"Senior" },
{ empid: 2, ename: "Ram", exp: 7, salary: 60000, hours: 7, jobtitle: "IT" },
{ empid: 3, ename: "Shyam", exp: 9, salary: 68000, hours: 6, jobtitle:
"ITSpecialist" },
{ empid: 4, ename: "Jay", exp: 5, salary: 56000, hours: 8, jobtitle: "Head" },
{ empid: 5, ename: "Rakesh", exp: 4, salary: 45000, hours: 8, jobtitle:
"WebDesigner" },
{ empid: 6, ename: "Mahesh", exp: 6, salary: 62000, hours: 8, jobtitle: "Backend"
},
{ empid: 7, ename: "Suresh", exp: 3, salary: 35000, hours: 8, jobtitle:
"WebDeveloper" },
{ empid: 8, ename: "Kaushal", exp: 3, salary: 38000, hours: 8, jobtitle: "Php" },
{ empid: 9, ename: "Arjun", exp: 1, salary: 31000, hours: 8, jobtitle: "Php" },
{ empid: 10, ename: "Purvesh", exp: 2, salary: 14000, hours: 3, jobtitle:
"Python" },
{ empid: 11, ename: "Raj", exp: 2, salary: 26000, hours: 9, jobtitle: "Python" },
{ empid: 12, ename: "Kirtan", exp: 2, salary: 29000, hours: 9, jobtitle:
"Java" },
{ empid: 13, ename: "Rupesh", exp: 2, salary: 21000, hours: 9, jobtitle:
"Java" },
{ empid: 14, ename: "Laxman", exp: 0, salary: 15000, hours: 9, jobtitle:
"Interns" },
{ empid: 15, ename: "Divay", exp: 0, salary: 17000, hours: 9, jobtitle: "Interns"
},
{ empid: 16, ename: "Taksh", exp: 1, salary: null, hours: 6, jobtitle: null }
]);

List all the names of the myemployees


db.myemployees.find({}, { ename: 1, _id: 0 });

List all the myemployees names start with R


db.myemployees.find({ ename: /^R/ });

List all the myemployees names starting with R or A


db.myemployees.find({ ename: { $regex: "^[RA]" } });

List all the Interns


db.myemployees.find({ jobtitle: "Interns" });

List all the myemployees whose experience is less


than 7 years but working hours should be 9
db.myemployees.find({ exp: { $lt: 7 }, hours: 9 });

List all the myemployees whose salary is higher than 50000


db.myemployees.find({ salary: { $gt: 50000 } });

List all the myemployees whose salary is lower than 30000


db.myemployees.find({ salary: { $lt: 30000 } });

Sort and display the results by salary in ascending order


db.myemployees.find().sort({ salary: 1 });

List all the myemployees by their working hours


db.myemployees.find().sort({ hours: 1 });

List all the myemployees Php Developers


db.myemployees.find({ jobtitle: "Php" });
Update the working hours of people whose experience is lower than 3 years. Decrease
the working hour by 1 hour
db.myemployees.updateMany({ exp: { $lt: 3 } }, { $inc: { hours: -1 } });

Update the salary of all the myemployees whose experience is 3 years and increase
their salary by 4000
db.myemployees.updateMany({ exp: 3 }, { $inc: { salary: 4000 } });

Display all the results where names are sorted alphabetically


db.myemployees.find().sort({ ename: 1 });

Create a view table where all the myemployees have experience above 8
db.createView("experienceAbove8", "myemployees",
[{ $match: { exp: { $gt: 8 } } }]);

Increase the salary of each employee by 10%


db.myemployees.updateMany({}, { $mul: { salary: 1.1 } });

Increase the experience of Interns by 1


db.myemployees.updateMany({ jobtitle: "Interns" }, { $inc: { exp: 1 } });

Display all the Java developers


db.myemployees.find({ jobtitle: "Java" });

Update the title of IT Specialist assistant to Junior IT Specialist and reduce the
working hour by 1
db.myemployees.updateMany({ jobtitle: "ITSpecialist" }, { $set: { jobtitle: "Junior
IT Specialist" }, $inc: { hours: -1 } });

Increase the salary of WebDesigner by 5%


db.myemployees.updateMany({ jobtitle: "WebDesigner" }, { $mul: { salary: 1.05 } });

Delete the records whose experience is 1 year


db.myemployees.deleteMany({ exp: 1 });

Delete all the records of Php Developers


db.myemployees.deleteMany({ jobtitle: "Php" });

Display all the results whose salary is less than the salary of Raj
const rajSalary = db.myemployees.findOne({ ename: "Raj" }).salary;
db.myemployees.find({ salary: { $lt: rajSalary } });

Display the record of the person whose salary is the highest


db.myemployees.find().sort({ salary: -1 }).limit(1);

Display the records of the person whose working hours is the least
db.myemployees.find().sort({ hours: 1 }).limit(1);

Display total number of myemployees and the total number of myemployees who got
salary
db.myemployees.aggregate([
{ $group: { _id: null, totalmyemployees: { $sum: 1 }, myemployeesWithSalary:
{ $sum: { $cond: [{ $ne: ["$salary", null] }, 1, 0] } } } }
]);

Display the records whose jobtitle is null


db.myemployees.find({ jobtitle: null });
Display the records of employee’s name, salary, and jobtitle whose jobtitle is not
null
db.myemployees.find({ jobtitle: { $ne: null } }, { ename: 1, salary: 1, jobtitle: 1
});

You might also like