Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
How to correctly create a reference between two models in mongodb using Mongoose?

The issue is that I have two Mongoose schema models - user and movie. A single movie will not be accessible for each user, so in the movie model I made a reference to the user as an array. On the frontend the user will have available a list of all his films. Additionally, I would like each user among the videos available to them to have the option to like a particular video.

I tried to add another reference in the user model with an array of favorite movies, but this solution seems too complicated when I try to implement this on the frontend. Do any of you have an idea how to do it better? I also thought to add a Boolean value by adding an isFavorite field to the movie model, but then all users would see that movie as favorite.

Movie Schema:

const movieSchema = mongoose.Schema(
  {
    user: [
      {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User',
      },
    ],
    name: {
      type: String,
      required: true,
    },
    url: {
      type: String,
    },
}

User Schema:

const userSchema = mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
    },
    password: {
      type: String,
      required: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda