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

MongoDB Query Modifiers Lab Exercise

The document contains a lab exercise on MongoDB query modifiers, divided into two levels: Student Database and Employee Database. Each level includes a series of queries with corresponding answers for tasks such as displaying specific fields, filtering records, sorting results, and limiting or skipping documents. The exercises cover various operations like finding students or employees based on age, department, experience, and salary.

Uploaded by

sunil
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)
11 views2 pages

MongoDB Query Modifiers Lab Exercise

The document contains a lab exercise on MongoDB query modifiers, divided into two levels: Student Database and Employee Database. Each level includes a series of queries with corresponding answers for tasks such as displaying specific fields, filtering records, sorting results, and limiting or skipping documents. The exercises cover various operations like finding students or employees based on age, department, experience, and salary.

Uploaded by

sunil
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
You are on page 1/ 2

MongoDB Query Modifiers - Lab Exercise with Answers

Level 1: Student Database

1. Display only the 'name' and 'grade' of all students.

Answer: db.students.find({}, {name: 1, grade: 1, _id: 0})

2. Retrieve all students who are older than 20.

Answer: db.students.find({age: {$gt: 20}})

3. Get all students who belong to the 'ECE' department.

Answer: db.students.find({department: 'ECE'})

4. Sort students by 'name' in ascending order.

Answer: db.students.find().sort({name: 1})

5. Sort students by 'age' in descending order.

Answer: db.students.find().sort({age: -1})

6. Limit the result to first 3 students.

Answer: db.students.find().limit(3)

7. Skip the first 2 documents and show the rest.

Answer: db.students.find().skip(2)

8. Find students with grade 'A' or 'B'.

Answer: db.students.find({grade: {$in: ['A', 'B']}})

9. Find students whose age is between 18 and 22.

Answer: db.students.find({age: {$gte: 18, $lte: 22}})

10. Project all students with 'name', excluding '_id'.

Answer: db.students.find({}, {name: 1, _id: 0})


MongoDB Query Modifiers - Lab Exercise with Answers

Level 2: Employee Database

1. Display only the 'name' and 'salary' of all employees.

Answer: db.employees.find({}, {name: 1, salary: 1, _id: 0})

2. List employees who have more than 2 years of experience.

Answer: db.employees.find({experience: {$gt: 2}})

3. Find employees with salary between 50,000 and 70,000.

Answer: db.employees.find({salary: {$gte: 50000, $lte: 70000}})

4. Find employees not in the 'HR' department.

Answer: db.employees.find({department: {$ne: 'HR'}})

5. Sort employees by 'salary' in descending order.

Answer: db.employees.find().sort({salary: -1})

6. Show only top 5 highest-paid employees.

Answer: db.employees.find().sort({salary: -1}).limit(5)

7. Retrieve employees whose names start with the letter 'S'.

Answer: db.employees.find({name: /^S/})

8. Project only 'emp_id' and 'department', excluding '_id'.

Answer: db.employees.find({}, {emp_id: 1, department: 1, _id: 0})

9. Use $in operator to find employees in 'IT' and 'Sales' departments.

Answer: db.employees.find({department: {$in: ['IT', 'Sales']}})

10. Skip the top 3 salaries and show the rest sorted by salary.

Answer: db.employees.find().sort({salary: -1}).skip(3)

You might also like