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

2K
Vistas
Remove referenced documents with mongoose/MongoDB middleware (in 2021 )

I tried all the solutions over stack overflow which relate to deleting referenced documents in MongoDB using node.js. But most of them either have solutions with deprecated methods and some of them do not work at all.

I have two models, Parent and Child with one-to-many relationship. The parent document includes array of referenced children documents id's as shown in the following schema.

//file => parent.js 

const parentSchema = new mongoose.Schema({
  parentName: {
    type: String,
  },
  childrens : [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Children'
    }
  ]
});

const Parent = mongoose.model("Parent", parentSchema);
module.exports = Parent ; 

Next file has children model with childrenSchema.pre() middleware or hook which is triggered successfully -

//file => children.js 

const mongoose = require("mongoose");
const Parent  = require("./parent"); 


const childrenSchema = new mongoose.Schema({
  childrenId: Number,
  info: String,
  type: String, 
});


childrenSchema.pre('findOneAndDelete', async function(next) {
  console.log('trigger when middleware runs' , this.getQuery()["_id"]) ; 

  const childrenId = new mongoose.Types.ObjectId(this.getQuery()["_id"])
  await Parent.updateOne({} , { $pull: { childrens: { _id: childrenId }}});
  next()
}) 


const Children = mongoose.model("Children", childrenSchema);
module.exports = Children ; 

Now, finally this is the route that I am calling to delete the instance of childrenId. I have got quite a few errors but mainly it says that Parent.updateOne()is not a function in the above file. But middleware is triggered successfully when the below route is executed.

// route which performs delete action and triggers mongoose middleware

router.delete('/delete/instance' , async(req,res) => {
  const childrenId = new mongoose.Types.ObjectId('612cd0d8f9553c9f243db691')
  
  const deletedInstance = await Children.findOneAndDelete(
      { _id: childrenId}
  )
  res.json(deletedInstance)

})
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Note that you are trying to perform updateOne() with no filter option. For the logic itself, why don't you put it inside the router:

const Parent  = require("./parent"); 

router.delete('/delete/instance' , async (req,res) => {
  await Parent.updateOne({ childrens: '612cd0d8f9553c9f243db691' }, { $pull: { childrens: '612cd0d8f9553c9f243db691' }})
  const deletedInstance = await Children.findOneAndDelete({ _id: '612cd0d8f9553c9f243db691'})
  res.json(deletedInstance)
})
over 4 years ago · Santiago Trujillo Denunciar
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