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

145
Visualizações
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
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