Lab Assignment 5
Lab Assignment 5
data.forEach(func on(df) {
var t = df.employeeNumber;
if (t > 1100) {
print(t);
});
2. Display Full Name of an Employee along with their total number of characters in their full
name.
data.forEach(func on(df) {
print(name);
print(name.length);
});
Write a MongoDB query to add a new a ribute called color to exis ng documents in a collec on
based on the parity of the age a ribute:
documents.forEach((doc) => {
});
Update all documents in the collec on by se ng the skills field based on the employee’s ID:
If the employee ID is odd, assign: ["Java", "Python", "C"]
db.student_details.find().forEach(employee => {
db.student_details.updateOne(
{ _id: employee._id },
);
});
db.student_details.find()
If the skills a ribute doesn’t exists in the document, during Update the skills a ribute will be created
automa cally with the assigned values.
</aside>
1. Find documents where the skills array has less than 3 elements:
javascript
CopyEdit
});
2. Find documents where the skills array has more than 3 elements:
javascript
CopyEdit
const queryGreaterThan3 = { "skills.3": { $exists: true } }; // Checks if the 4th element exists
});
javascript
CopyEdit
});
javascript
CopyEdit
});
3. Find documents where at least one skill contains the le er P anywhere (not at the start or end):
javascript
CopyEdit
const queryContainsP = { skills: { $elemMatch: { $regex: /P.+P/, $op ons: "i" } } };
});
Write a query to Update the salaries of all employees by 10% if the salary is greater than 20,000. In
case, there exists no a ribute called salary, create an a ribute salary with a default value of 3000
assigned to it.
db.employees.updateMany(
);
db.employees.updateMany(
);
Display the quan ty of colors of a color company where the stock of colors is greater than 4. Only
display top 3 stocks
db.colorCollec on.aggregate([
{ $sort: { stock: -1 } },
{ $limit: 3 },
]);