q3 Mongo Emp
q3 Mongo Emp
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 }
]);
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 } });
Create a view table where all the myemployees have experience above 8
db.createView("experienceAbove8", "myemployees",
[{ $match: { exp: { $gt: 8 } } }]);
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 } });
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 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] } } } }
]);