Mongoose dynamic schema type based on other field value

Viewed 63

Let say I have the following schema:

new Schema({
  fruitType: {
    type: String,
    enum: ['apple', 'orange', 'melon', 'mango'],
    required: true,
  },
  fruitValue: [ appleSchema ],
})

When the fruitType is apple, I want the fruitValue to be appleSchema and when it is melon, I want the field to have melonSchema. Is there a way in Mongoose to achieve this?

0 Answers
Related