Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
many-to-many relationship cascade update (add or remove an element in a field of array type of ID) in mongoose?

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda