I am new to mongoose and I have searched alot about it and was not able to find out the ans. Please help, thanks in advance.
const user = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
rollno: {
type: Number,
required: true,
unique: true,
},
password : {
type: String,
required: true,
},
isVoter: {
type: Boolean,
required: true,
default: true
}
}, {
timestamps: true
});
Can I have a schema which would be dependent on the value of isVoter. For example if value of isVoter is false then we should have schema like this :
const user = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
rollno: {
type: Number,
required: true,
unique: true,
},
password : {
type: String,
required: true,
},
isVoter: {
type: Boolean,
required: true,
default: true
},
promises: [
{
type: String
}
]
, {
timestamps: true
});
you can use
pre
hook in mongoose, check the documentation and before saving, check theisVoter
value, if you don't want to save promises, trythis.promises = undefined
and in the schema promises should be definded