How to populate different models on the basis of conditions in mongoose?

Viewed 41

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

0 Answers
Related