Home > Software engineering >  pull operation in mongoose is taking several attempts to delete a sub document
pull operation in mongoose is taking several attempts to delete a sub document

Time:01-02

i am developing a todolist application so that for each user i store the activities of a user in an array. schema of user look like this.

const activitySchema=new mongoose.Schema({
  activity:String
})

const Activity=new mongoose.model("activity",activitySchema)
const userSchema=new mongoose.Schema({
  username:String,
  password:String,
  activities:[],
});

i used pull command to delete a specific activity from user actvities list

app.post("/",function(req,res){
  var activity=new Activity({activity:req.body.activity});
  User.updateOne({username:req.user.username},{"$addToSet":{activities:activity}},function(err){
    if(err)
    console.log(err)
    else {
      res.redirect("/")
    }
  })
})

this code is working after user clicks for 5-10 times on delete button but not immediately. the log output showing matched document:1 and modified document:0. kindly assist

CodePudding user response:

For Modifying the document you need to save the model after making every change to the model.

activity.save().then(()=>{ console.log("Document Saved"))

Similar is the for the other document where you are making a change, You can read more about it if you want Mongoose

CodePudding user response:

This is a very useful and informative post. I always follow this website. I recommend using this website for unique blog post. Please follow this website and rank higher on google. Read More Blog Posts

  • Related