Share
Explore

LAB Test 2 W23 Mad 6123 Version A

Building the Back End Data Store for the Node.js Full Stack Web Application


Scenario


You have been tasked with building the back end data store for a college enrollment system. The system has three collections: students, courses, and enrollments. Each student can enroll in multiple courses, and each course can have multiple students enrolled in it. The enrollments collection stores the association between students and courses. You need to implement a MongoDB predicate join between the students and enrollments collections, and between the courses and enrollments collections to retrieve a list of enrolled students for each course and a list of enrolled courses for each student.

Sample Data

Students:
Ashley
Jordan
Mary
Courses:
Math
Database
NODEJS
Java

Enrollments:
Mary enrolls in Java, Database, and Math
Ashley enrolls in NODEJS, Math, and Database
Jordan enrolls in Math, Java, and Database

Instructions


Create a new MongoDB database using the following command:
mongo college


Create three collections for students, courses, and enrollments using the following commands:
db.createCollection("students")
db.createCollection("courses")
db.createCollection("enrollments")


Insert the sample data into the collections using the following commands:

db.students.insertMany([
{ name: "Ashley" },
{ name: "Jordan" },
{ name: "Mary" }
])

db.courses.insertMany([
{ name: "Math" },
{ name: "Database" },
{ name: "NODEJS" },
{ name: "Java" }
])

db.enrollments.insertMany([
{ student_name: "Mary", course_name: "Java" },
{ student_name: "Mary", course_name: "Database" },
{ student_name: "Mary", course_name: "Math" },
{ student_name: "Ashley", course_name: "NODEJS" },
{ student_name: "Ashley", course_name: "Math" },
{ student_name: "Ashley", course_name: "Database" },
{ student_name: "Jordan", course_name: "Math" },
{ student_name: "Jordan", course_name: "Java" },
{ student_name: "Jordan", course_name: "Database" }
])


Use the $lookup operator to join the enrollments collection with the students and courses collections to retrieve a list of enrolled courses for each student and a list of enrolled students for each course.

db.students.aggregate([
{ $lookup:
{
from: "enrollments",
localField: "name",
foreignField: "student_name",
as: "enrollments"
}
}
])

db.courses.aggregate([
{ $lookup:
{
from: "enrollments",
localField: "name",
foreignField: "course_name",
as: "enrollments"
}
}
])



In this lab test, you demonstrated your ability to build the back end data store for a Node.js full stack web application using MongoDB.

You implemented a predicate join between collections to retrieve a list of enrolled courses for each student and a list of enrolled students for each course.

You have also demonstated your ability to work with sample data for students and courses and instructions on how to make a MongoDB database and implement this join using the $lookup operator.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.