Assignments Of Day 14: MongoDB Query Operators

Rashmi Mishra
0

 Assignments Of  Day 14: MongoDB Query Operators

Assignment 1: Find all students older than 18 years

Task: Write a query to find all students whose age is greater than 18 using the $gt operator.

Solution:

javascript

CopyEdit

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

Explanation: This query selects all students whose age is greater than 18. The $gt operator stands for "greater than."


Assignment 2: Find all students younger than 25 years

Task: Write a query to find all students whose age is less than 25 using the $lt operator.

Solution:

javascript

CopyEdit

db.students.find({ age: { $lt: 25 } })

Explanation: This query selects all students whose age is less than 25. The $lt operator stands for "less than."


Assignment 3: Find students with age in the range [20, 22]

Task: Write a query to find students whose age is between 20 and 22 using the $gt and $lt operators.

Solution:

javascript

CopyEdit

db.students.find({ age: { $gt: 19, $lt: 23 } })

Explanation: This query selects all students whose age is greater than 19 and less than 23. The $gt operator checks for ages greater than 19, and the $lt operator checks for ages less than 23.


Assignment 4: Find students who have a major in either "Biology" or "Physics"

Task: Write a query to find students whose major is either "Biology" or "Physics" using the $in operator.

Solution:

javascript

CopyEdit

db.students.find({ major: { $in: ["Biology", "Physics"] } })

Explanation: This query selects all students whose major is either "Biology" or "Physics". The $in operator checks if the major field matches any of the values in the provided array.


Assignment 5: Find students whose name starts with the letter "A"

Task: Write a query to find students whose name starts with "A" using the $regex operator.

Solution:

javascript

CopyEdit

db.students.find({ name: { $regex: /^A/ } })

Explanation: This query selects all students whose name starts with the letter "A". The $regex operator allows for regular expression pattern matching, and ^A means the name starts with the letter "A".


Assignment 6: Find students whose name ends with the letter "n"

Task: Write a query to find students whose name ends with the letter "n" using the $regex operator.

Solution:

javascript

CopyEdit

db.students.find({ name: { $regex: /n$/ } })

Explanation: This query selects all students whose name ends with the letter "n". The $regex operator performs pattern matching, and n$ means the name ends with the letter "n".


Assignment 7: Find students who are not enrolled in "Computer Science"

Task: Write a query to find students whose major is not "Computer Science" using the $ne operator.

Solution:

javascript

CopyEdit

db.students.find({ major: { $ne: "Computer Science" } })

Explanation: This query selects all students whose major is not "Computer Science". The $ne operator means "not equal."


Assignment 8: Find students who have a scholarship field

Task: Write a query to find students who have a scholarship field using the $exists operator.

Solution:

javascript

CopyEdit

db.students.find({ scholarship: { $exists: true } })

Explanation: This query selects all students who have a scholarship field. The $exists operator checks for the presence of the specified field.


Assignment 9: Find students who have no scholarship field

Task: Write a query to find students who do not have a scholarship field using the $exists operator.

Solution:

javascript

CopyEdit

db.students.find({ scholarship: { $exists: false } })

Explanation: This query selects all students who do not have a scholarship field. The $exists: false checks for the absence of the specified field.


Assignment 10: Find students whose name contains "Smith"

Task: Write a query to find students whose name contains the substring "Smith" using the $regex operator.

Solution:

javascript

CopyEdit

db.students.find({ name: { $regex: /Smith/ } })

Explanation: This query selects all students whose name contains the substring "Smith". The $regex operator allows for pattern matching, and Smith matches any occurrence of the string "Smith" in the name field.


Summary of Solutions:

1.   Find all students older than 18 years:

javascript

CopyEdit

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

2.   Find all students younger than 25 years:

javascript

CopyEdit

db.students.find({ age: { $lt: 25 } })

3.   Find students with age in the range [20, 22]:

javascript

CopyEdit

db.students.find({ age: { $gt: 19, $lt: 23 } })

4.   Find students with a major in either "Biology" or "Physics":

javascript

CopyEdit

db.students.find({ major: { $in: ["Biology", "Physics"] } })

5.   Find students whose name starts with "A":

javascript

CopyEdit

db.students.find({ name: { $regex: /^A/ } })

6.   Find students whose name ends with "n":

javascript

CopyEdit

db.students.find({ name: { $regex: /n$/ } })

7.   Find students who are not enrolled in "Computer Science":

javascript

CopyEdit

db.students.find({ major: { $ne: "Computer Science" } })

8.   Find students who have a scholarship field:

javascript

CopyEdit

db.students.find({ scholarship: { $exists: true } })

9.   Find students who have no scholarship field:

javascript

CopyEdit

db.students.find({ scholarship: { $exists: false } })

10.                     Find students whose name contains "Smith":

javascript

CopyEdit

db.students.find({ name: { $regex: /Smith/ } })

These assignments help students practice using different MongoDB query operators like $gt, $lt, $in, $regex, and $exists, which are essential for advanced querying in MongoDB.



Post a Comment

0Comments

Post a Comment (0)

About Me