Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda