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

148
Views
How to implement dynamically changing content type in schema

I have this problem where I want one of my key in my Schema to contain an array of other types of "questions".

Below is my main schema.

const forumsSch = new mongoose.Schema({
  summary: {
    type: String,
    required: true,
  },
  colorTheme: {
    type: String,
    enum: {
      values: ['dark-Blue', 'dark-Yellow', 'light-Green'],
      message: 'please enter a valid colorTheme',
    },
  },
  colorCustom: {
    type: [String],
  },
  questions: [
    /*
    (User request sample) 
    {
     type: singleQuestion,
     title: sample 1,
     maxSelect: 5
    },
    {
     type: multipleQuestion
     title: sample 1,
     imagesRepresent: true
    }
  */
  ],
});

When user creates a document with its "questions" key as an array of questions (type specified in key "type") the corresponding schema automatically validates if each of them fit the requirements

below are the corresponding schemas

const singleQuestionSch = new mongoose.Schema({
  title: {
    type: String,
    required: [true, 'Please specify title for your Question'], //validator
  },
  maxSelect: {
    type: Number,
    required: true,
  },
});



const multipleQuestionSch = new mongoose.Schema({
  title: {
    type: String,
    required: [true, 'Please specify title for your Question'], //validator
  },
  imagesRepresent: Boolean,
});

How do implement this?

about 4 years ago · Juan Pablo Isaza
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!