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

Lab Assignment 5

The document outlines various MongoDB queries for managing employee data, including displaying employees based on their employee numbers, calculating full name lengths, and adding attributes based on age. It also includes updates to employee skills based on their ID parity and queries to filter documents based on the number of skills and specific skill characteristics. Additionally, it provides examples of updating salaries and aggregating color stocks from a collection.

Uploaded by

diptendupal232
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Assignment 5

The document outlines various MongoDB queries for managing employee data, including displaying employees based on their employee numbers, calculating full name lengths, and adding attributes based on age. It also includes updates to employee skills based on their ID parity and queries to filter documents based on the number of skills and specific skill characteristics. Additionally, it provides examples of updating salaries and aggregating color stocks from a collection.

Uploaded by

diptendupal232
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Assignment 5

1. Display All Employees whose employeeNumber is greater than 1100.

var data = db.student_details.find();

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.

var data = db.student_details.find();

data.forEach(func on(df) {

var name = df.firstName + " " + df.lastName;

print(name);

print(name.length);

});

Add a New A ribute color Based on age

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:

const documents = await collec on.find({ age: { $exists: true } }).toArray();

documents.forEach((doc) => {

const color = doc.age % 2 === 0 ? "Green" : "Red";

collec on.updateOne({ _id: doc._id }, { $set: { color } });

});

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"]

 If the employee ID is even, assign: ["JavaScript", "HTML", "Tailwind"]

db.student_details.find().forEach(employee => {

let skills = (employee.employeeNumber % 2 === 0)

? ["JavaScript", "HTML", "Tailwind"]

: ["Java", "Python", "C"];

db.student_details.updateOne(

{ _id: employee._id },

{ $set: { skills: skills } }

);

});

db.student_details.find()

<aside> <img src="/icons/verified_green.svg" alt="/icons/verified_green.svg" width="40px" />

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

const queryLessThan3 = { skills: { $size: 2 } }; // Matches exactly 2 elements

collec on.find(queryLessThan3).toArray((err, docs) => {

if (err) throw err;

console.log("Documents with less than 3 skills:", docs);

});

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

collec on.find(queryGreaterThan3).toArray((err, docs) => {

if (err) throw err;

console.log("Documents with more than 3 skills:", docs);

});

1. Find documents where at least one skill starts with the le er P:

javascript

CopyEdit

const queryStartsWithP = { skills: { $elemMatch: { $regex: /^P/, $op ons: "i" } } };

collec on.find(queryStartsWithP).toArray((err, docs) => {

if (err) throw err;

console.log("Documents where at least one skill starts with 'P':", docs);

});

2. Find documents where at least one skill ends with the le er T:

javascript

CopyEdit

const queryEndsWithT = { skills: { $elemMatch: { $regex: /T$/, $op ons: "i" } } };

collec on.find(queryEndsWithT).toArray((err, docs) => {

if (err) throw err;

console.log("Documents where at least one skill ends with 'T':", docs);

});

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" } } };

collec on.find(queryContainsP).toArray((err, docs) => {

if (err) throw err;

console.log("Documents where at least one skill contains 'P' in between:", docs);

});

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(

{ salary: { $exists: false } },

{ $set: { salary: 3000 } }

);

db.employees.updateMany(

{ salary: { $gt: 20000 } },

{ $mul: { salary: 1.10 } }

);

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([

{ $match: { stock: { $gt: 4 } } },

{ $sort: { stock: -1 } },

{ $limit: 3 },

{ $project: { _id: 0, company: 1, stock: 1 } }

]);

You might also like