7

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)

})
relu
  • 333
  • 1
  • 3
  • 18

1 Answers1

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)
})
relu
  • 333
  • 1
  • 3
  • 18
NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • I am aware of this solution and it works. However, I would like to understand and use middleware hooks so that I can keep the logic of cleaning the orphaned reference items out of controller/route. I appreciate your input. – relu Aug 31 '21 at 11:14