Relationship 1:N mongoDB

Asked

Viewed 29 times

0

I am creating a streaming application similar to Netflix and would like to know what would be the best way to treat "relationships" in Mongo. (I don’t know if using a bank in the QTS is the best option, but I don’t care, it’s only for matters of study even...).

When it comes to posting movies, it’s quiet. Just create in a model, but when it comes to series(Breaking bad, Friends etc.) what would be the best way? since a series has several episodes and these in turn will probably have to have an ID for every time the user clicks one of them to be redirected to the tab with the ID of that episode. My question is the following: it is better to create a new MODEL for each episode posted (I believe it is not the best way, since that way would end up having several models and if it was a large application would become unviable). Or is it ideal to have an array type field inside the model of each series posted? Example:

serieSchema = new mongoose.Schema({
title: String,
episodes: Array
)}

this way I see more sense, but I can’t think of how I would pass an ID for each new episode posted.I’m sorry if I got confused, I appreciate anyone who can help me with the reasoning.

1 answer

1


Oops! Well, I don’t have an exact idea about how you’re thinking about modeling but according to the Mongodb* people the recommendation for 1:N relationships is embedding rather than referencing, that is, putting the N part inside the 1, something like:

{
  __id:...,
  title: "Nome da Série",
  year: 1999,
  mainCast: [ ... ],
  totalSeasons: 9,
  totalEpisodes: 9,
  ...
  episodeList: {
    1: {
      season: 1,
      cast: [ ... ],
      ...
}}}

The exception of this rule are the 1:Z (1 for zillion, Big Data staff).

(*) The source is the course M320 - Data Modeling from Mongodb University which presents design standards for database modeling. Incidentally, the course is free.

  • Thank you so much for your help and for providing the source :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.