I am facing some issue here i am try to store only one object either school or college or work, but mongoose automatically create other two object.
how to omit other object. here i should use default
let mySchema = new Schema({
"type": { type: String, default: "" }, // school or college or work
"school": {
'name':{ type: String, default: "" },
'parent':{ type: String, default: "" },
'address':{ type: String, default: "" },
'email_id':{ type: String, default: "" },
'phone_no':{ type: String, default: "" },
},
"college": {
'name':{ type: String, default: "" },
'friend':{ type: String, default: "" },
'address':{ type: String, default: "" },
'email_id':{ type: String, default: "" },
'phone_no':{ type: String, default: "" },
},
"work": {
'name':{ type: String, default: "" },
'colleague':{ type: String, default: "" },
'address':{ type: String, default: "" },
'email_id':{ type: String, default: "" },
'phone_no':{ type: String, default: "" },
},
});
2
Answers
Is something avoiding you to set this programmatically?
If you check this section from Mongoose docs, you will see that Mongoose always creates an empty object for nested documents (e.g.
school
in your case).You could use a subdocument instead, where
school
,college
andwork
are instances ofSchema
.What you can do: