Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!