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

144
Visualizações
How to populate different models on the basis of conditions in mongoose?

A comment can have 2 types of author, first one is user and second is listing.

comment.js

  commentSchema={ _author: {
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'author'
    },
    author: {
        type: String,
        required: true,
        enum: ['User', 'Listing']
    },text: {
        type: String,
        required: true,
        trim: true
    }}

now users have their profile details inside general profile model and listings have their profile details inside listing information model

userModel.js {_generalProfile:ObjectId}

listingModel.js {_listingInformation:ObjectId}

generalProfile.js {name:String}

listingInformation.js {title:String}

now when fetching a comment then how to populate user and listing data, means if author is user then populate generalProfile and if author is listing then populate listingInformation

how to do this ?

Thanks

over 4 years ago · Santiago Trujillo
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