I hope this question finds you well. I have two models, 1st model of Faculty is as following:
const facultySchema = new mongoose.Schema(
{
_id: mongoose.Schema.Types.ObjectId,
name: {
type: String,
required: [true, "le nom de la faculté est obligatoire"],
},
code_name: {
type: String,
required: [true, "l‘abréviation est obligatoire"],
},
programs: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Program",
},
],
}
);
and here Program model
const programSchema = new mongoose.Schema(
{
_id: mongoose.Schema.Types.ObjectId,
name: {
type: String,
required: [true, "le nom est obligatoire"!],
},
faculties: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Faculty",
},
],
},
{
timestamps: true,
}
);
I want to add a middleware so that when I update a program by removing or adding an element in the faculties array, it will be automatically removed or added in programs field according to the faculty_id removed. and vis-versa if a faculty is updated by removing or adding an element in programs field, the faculty should be added or removed accordingly in the faculties field in the program related. I know about the pre and post middleware but I don't know how to do this specific update cascading please help me, this is really important to me. thank you so much.