I am trying to make a booking system for a cinema with MongoDB.
I keep all the information about the movie in a model Movie
, including title
, description
, actors
, rating
, director
, etc.
However, a movie can be shown in the cinema in different rooms and at different dates, so I also have a model Showtime
, where I have room
, price
, and date
.
Finally, I also need a model Ticket
with fields purchasedBy
, purchasedAt
, isRedeemed
, etc.
However, I don't know how to link the models and extract the showtimes. I want a list on the frontpage with all the movies (with title, description and image), but I also want to show the date and price. The problem is that date and price can vary since each movie can have multiple (and different) dates and prices, so I just want to show the smallest price and soonest date.
At the moment, I have a schema that looks like something like
movieSchema = Schema({
name: String,
description: String,
image: String,
showtimes: [{ type: ObjectId, ref: 'Showtime' }]
});
I could get a date and price by just taking the first showtime in the array of showtimes
Movie.find().populate('showtimes').then(movies => {
movies.forEach(movie => {
console.log(movie.showtimes[0].date)
console.log(movie.showtimes[0].price)
});
});
However, I need to sort by the most recent date and/or the lowest price, so I am not sure if my data structure is appropriate for this purpose.
What would be ideal, would be to be able to do something like this:
Movie.find().sort('showtimes.date showtimes.price').populate('showtimes').then(movies => {
...
});
but since I am only storing the IDs of the showtimes in my showtimes
field, this is not possible.
Alternatively, I could change the schema to
showtimeSchema = Schema({
date: Date,
price: Number
});
movieSchema = Schema({
name: String,
description: String,
image: String,
showtimes: [showtimeSchema]
});
so I don't have to use populate()
. However, the problem is that when a customer buys a ticket, I need to refer to the showtime in the ticket object, so I need a model for showtimes on its own.
Edit
As mentioned in the comments, it might be clever to embed the documents directly in movieSchema
. However, I don't know what my Ticket
model should look like.
Right now it is something like
ticketSchema = Schema({
showtime: { type: ObjectId, ref: 'Showtime' }
purchasedAt: Date,
purchasedBy: { type: ObjectId, ref: 'User' }
isRedeemed: Boolean
})
So when I am printing the ticket, I have to do something like
Ticket.findById(ticketId).populate({
path: 'Showtime',
populate: {
path: 'Movie'
}
}).then(ticket => {
console.log(ticket.date);
console.log(ticket.event.name);
});