0% found this document useful (0 votes)
44 views5 pages

MongoDB Employee Data Updates

The document contains 25 questions and solutions related to updating employee documents in a MongoDB database collection. The updates include modifying field values, adding/removing projects and skills, incrementing/decrementing values, and filtering documents based on field criteria.

Uploaded by

Vidisha singhal
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)
44 views5 pages

MongoDB Employee Data Updates

The document contains 25 questions and solutions related to updating employee documents in a MongoDB database collection. The updates include modifying field values, adding/removing projects and skills, incrementing/decrementing values, and filtering documents based on field criteria.

Uploaded by

Vidisha singhal
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

Name- Ankur Gautam Section: D-(10) University Roll-No: 201500102

Full Stack Assignment

Q1. All Employee’s with the desg as ‘CLERK’ are now called as (AO)
Administrative Officers. Update the Employee collection for this.

Sol. [Link]({ designation: "CLERK" },{ $set: { designation:


"AO" } })

Q2. Change the number of hours for project-1 to 5 for all employees
with designation analyst.
Sol. [Link]({ "designation": "analyst" },{ $set: { "project-
[Link]": 5 } })

Q3. Add 2 projects project-3 and project-4 for employee whose name
starts with ”Deep” with 2 hrs
Sol. [Link]({ name: { $regex: /^Deep/ } },{ $push: {
projects: { name: "project-3", hours: 2 } } },{ $push: { projects: { name:
"project-4", hours: 2 } } })

Q4. Add bonus rs 2000 for all employees with salary > 50000 and
1500 if salary <50000 and > 30000 otherwise bonus will be 1000

Sol. [Link]({}, [{$set: {bonus: {$cond: {if: { $gte:


["$salary", 50000] },then: 2000, else: {$cond: {if: { $gte: ["$salary", 30000]
},then: 1500, else: 1000}}}}}}])

Q5. Change manager name to Tushar for all employees whose


manager is currently “satish” And manager number to 3333
Sol. [Link]({ manager: "satish" },{ $set: { manager:
"Tushar", managerNumber: 3333 } })

Q6. Increase salary of all employees from “purchase department” by


15000
Sol. [Link]({ department: "purchase department" },{ $inc:
{ salary: 15000 } })

Q7. Decrease number of hrs by 2 for all employees who are working
on project-2
Sol. [Link]({ project: "project-2" },{ $inc: { hrs: -2 } })

Q8. Delete project-2 from all employee document if they are working
on the project for 4 hrs.

Sol. [Link]( { "[Link]": "project-2",


"[Link]": 4 },{ $pull: { projects: { projectName: "project-2" } }
})

Q9. Change the salary of employees to 10000 only if their salary is <
10000

Sol. [Link]({ salary: { $lt: 10000 } },{ $set: { salary:


10000 } })

Q10. Increase bonus of all employees by 500 if the bonus is <2000 or


their salary is < 20000 or if employee belong to sales department

Sol. [Link]({$or: [{ bonus: { $lt: 2000 } },{ salary: { $lt:


20000 } },{ department: "Sales" }]},{ $inc: { bonus: 500 } })

Q11. Add 2 new project at position 2 for all employees with


designation analyst or salary is equal to either 30000 or 33000 or
35000

Sol. [Link]({$or: [{ designation: "analyst" },{ salary: { $in:


[30000, 33000, 35000] } }] },{$push: {projects: {$each: [{ name: "New Project
1", position: 2 }, { name: "New Project 2", position: 2 }],$position: 2}}})

Q12. Delete last project of all employees with department name is


“HR” and if the location is Mumbai
Sol. [Link]({ department: "HR", location: "Mumbai" },{
$pop: { projects: 1 } })

Q13. Change designation of all employees to senior programmer if


they are working on name:”Project-1” for 4 hrs

Sol. [Link]({ name: "Project-1", hours_worked: { $gte: 4 }


},{ $set: { designation: "Senior Programmer" } })

Q14. Add list of hobbies in all employees document whose manager is


Rajan or Revati

Sol. [Link]( { $or: [ { manager: "Rajan" }, { manager:


"Revati" } ] }, { $set: { hobbies: [ "hobby1", "hobby2", "hobby3" ] } })

Q15. Add list of skillset in all employee documents who are working
on project-4 for 3 hrs or on project-3 for 4 hrs

Sol. [Link]({$or: [{"[Link]": “Project-3”,


"[Link]": 4},{"[Link]": “Project-4”, "[Link]":
3}]},{$addToSet: {skills: {$each: ["MongoDB", "AngularJS"]}}})

Q16. Add a new hobby as blogging at 3 position in hobbies array for


all employess whose name starts with R or p and ends with j or s
Sol. [Link]({$and: [{ name: { $regex: /^R|^P/ } },{ name: {
$regex: /j$|s$/ } }] }, { $push: { hobbies: { $each: [ "blogging" ], $position: 2 }
} })

Q17. Increase salary by 10000 for all employees who are working on
project-2 or project-3 or project-1 Decrease bonus by 1000 rs And
increase salary by 1000rs for all employees whose department
location is Mumbai

Sol. [Link]({ projects: { $in: ["project-1", "project-2",


"project-3"] } },{ $inc: { salary: 10000 } })

[Link]({ departmentLocation: "Mumbai" },{ $inc: { bonus:


-1000, salary: 1000 } })
Q18. Remove all employees working on project-1

Sol. [Link]({ “[Link]”: "project-1" })

Q19. Replace document of employee with name “Deepak to some new


document
Sol. [Link]({ name: "Deepak" },{$set: {name: "New
Name",age: 25,department: "New Department"}})

Q20. Change skill python to python 3.8 for all employees if python is
there in the skillset
Sol. [Link]({ skills: "python" },{ $set: { "skills.$": "python
3.8" } })

Q21. Add 2 skills MongoDb and Perl at the end of skillset array for all
employees who are working at Pune location
Sol. [Link]({ location: "Pune" },{ $push: { skillset: {
$each: ["MongoDb", "Perl"] } } })

Q22. Delete first hobby from hobby array for all employees who are
working on project-1 or project-2

Sol. [Link]({"projects": {"$elemMatch": {"name": {"$in":


["project-1","project-2"]}}}},{"$pop": {"hobbies": -1}})

Q23. Delete last hobby from hobbies array for all employees who are
working on project which is at 2 nd position in projects array for 4
hrs

Sol. [Link]({projects: { $size: 2 }, "[Link]": 4},{


$pop: { hobbies: 1 } })

Q24. Add 2 new projects at the end of array for all employees whose
skillset contains Perl or python
Sol. [Link]({ "skillset": { $in: ["Perl", "Python"] } },{
$push: { "projects": { $each: ["New Project 1", "New Project 2"] } } })
Q25. Change hrs to 6 for project-1 for all employees if they working
on the project-1 for < 6 hrs. otherwise keep the existing value.
Sol. [Link]({ "project": "project-1", "hrs": { $lt: 6 } },{
$set: { "hrs": 6 } })

You might also like