0

I'm trying to figure out how to best design a Schema base on the value of a checkbox:

var weatherSchema = mongoose.Schema({
day: Date,
minTtemperature: Number,
maxTemperature: Number,
snowy: Boolean,
amountOfSnow: Number,
snowAttribute2: String,
snowAttribute3: String

 })

What i want to accomplish is to fill Snow attributes only in days where snowy value is True.

I've explored discriminators but I think this option is only helpful when I want to add different attributes or schema fields based on the value of an input. I'll really appreciate any help.

1 Answer 1

1

I would argue that discriminators is probably what you should consider.

Here is why:

  1. You will be using model inheritance. Easy to add common fields later on. Your data seems to be dependent on the type of day and seems to have common fields (like day, temperature etc) e.g. (sunny, stormy, snowy) so that type descriminator would be helpful.
  2. You will have a descriminator key lets call it dayType with which you can now differentiate the type of days. This will also allow you to easily count/query how many sunny or snowy days you had in a date period with ease.
  3. Based on that checkbox you can simply switch the models from one to the other where each would have its own specific data. Snow days would have amountOfSnow etc.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Akrion for explaining this. I'm going to try this approach.
One question... In my case I have to discriminate base on two checkbox, Can I have more than one key discriminator?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.