Building the Back End Data Store for the Node.js Full Stack Web Application: Lab Test
Scenario:
You are building a Node.js full stack web application for a movie theater that allows customers to purchase tickets online. You need to create a back end data store using MongoDB to store information about movie theaters, movie showtimes, and ticket sales.
Sample Data:
Showtime 1: 2022-05-01 18:00:00 Showtime 2: 2022-05-01 20:00:00 Showtime 3: 2022-05-01 22:00:00
Instructions:
Create a MongoDB database called "movie_tickets". Create a collection called "theaters" with the following fields:
Insert the sample data for the two theaters. Create a collection called "movies" with the following fields:
Insert the sample data for the three movies. Create a collection called "showtimes" with the following fields:
Insert the sample data for the three showtimes for each theater and movie. Create a collection called "purchasers" with the following fields:
Insert the sample data for the three purchasers. Create a collection called "tickets" with the following fields: Insert some sample data for ticket sales for each showtime and purchaser. Use the $lookup operator to join the collections and retrieve a list of tickets purchased by each purchaser and a list of showtimes for each theater.
Sample Code:
db.purchasers.aggregate([
{ $lookup:
{
from: "tickets",
localField: "_id",
foreignField: "purchaser_id",
as: "tickets"
}
}
])
db.theaters.aggregate([
{ $lookup:
{
from: "showtimes",
localField: "_id",
foreignField: "theater_id",
as: "showtimes"
}
}
])
Conclusion:
In this lab, we have learned how to build the back end data store for a Node.js full stack web application using MongoDB for a movie theater ticket purchasing system. We have implemented a join between collections to retrieve a list of tickets purchased by each purchaser and a list of showtimes for each theater. We have also provided sample data for theaters, movies, showtimes, purchasers, and ticket sales, and instructions on how to make a MongoDB database and implement this join using the $lookup operator.