MongoDB Student Relation Assignment
---
Step 1: Create a MongoDB Collection
Insert the following student data into the `students` collection:
To begin, we first create a MongoDB collection named `students` and insert multiple student
records. This collection will store details such as student name, course, and age.
use university; // Switch to or create the database
db.students.insertMany([
{ _id: 1, name: "Alice", course: "Data Science", age: 22 },
{ _id: 2, name: "Bob", course: "Computer Science", age: 23 },
{ _id: 3, name: "Charlie", course: "Data Science", age: 21 },
{ _id: 4, name: "David", course: "AI & ML", age: 24 },
{ _id: 5, name: "Emma", course: "Data Science", age: 22 },
{ _id: 6, name: "Frank", course: "Cybersecurity", age: 25 },
{ _id: 7, name: "Grace", course: "Data Science", age: 23 },
{ _id: 8, name: "Henry", course: "Software Engineering", age: 24 }
]);
Step 2: Solve the Assignment Queries
a. Display all documents
This query retrieves all documents (students) from the `students` collection and formats the
output for better readability.
db.students.find().pretty();
b. Display all students in ‘Data Science’
This query filters the collection to show only students who are enrolled in the "Data
Science" course.
db.students.find({ course: "Data Science" }).pretty();
c. Display 5th, 6th, and 7th student
To retrieve a specific range of students, we use the `skip()` method to bypass the first four
students and `limit(3)` to fetch only the next three students.
db.students.find().skip(4).limit(3).pretty();
Bonus Queries
1. Count the Number of Students in Each Course
This query groups students based on their courses and counts the number of students in
each course.
db.students.aggregate([
{ $group: { _id: "$course", totalStudents: { $sum: 1 } } }
]);
2. Find Students Who Are 23 Years Old or Older
This query retrieves all students who are at least 23 years old.
db.students.find({ age: { $gte: 23 } }).pretty();
3. Display Students Sorted by Age (Descending)
Sorting students by age in descending order to show the oldest students first.
db.students.find().sort({ age: -1 }).pretty();
4. Retrieve Students Whose Names Start with 'A'
Using a regular expression to find students whose names start with the letter 'A'.
db.students.find({ name: { $regex: /^A/i } }).pretty();
5. Find the Youngest and Oldest Student
Finding the youngest and oldest students in the database.
// Youngest Student
db.students.find().sort({ age: 1 }).limit(1);
// Oldest Student
db.students.find().sort({ age: -1 }).limit(1);
6. Display the Total Number of Students
This query returns the total number of students in the `students` collection.
db.students.countDocuments();
7. Delete Students Enrolled in 'Cybersecurity'
This query removes all students who are enrolled in the "Cybersecurity" course.
db.students.deleteMany({ course: "Cybersecurity" });
8. Update the Course of a Student
Updating a specific student's course, in this case, changing Emma’s course to "Machine
Learning".
db.students.updateOne(
{ name: "Emma" },
{ $set: { course: "Machine Learning" } }
);
End of Assignment