0

From this post, I know that I cannot accomplish what I need with the code I currently have shown below. However, all of the documentation I have come across doesn't give a use case of needing to provide an OR condition to the remove() function in mongoose and I am not sure how that would be structured. I need the following to work somehow...

router.delete('/friend/:id',function(req,res){
    var response = new Response();
    Message.find().or([{'from.id':req.user.id},{'to.id':req.user.id}]).exec(function(err,messages){
        _.each(messages,function(message){
                message.remove(function (err,message) {

                })
            });
        });
    res.send(response.complete());
});
Community
  • 1
  • 1
TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208

1 Answers1

2

There are two remove methods: Model#remove (the instance method), and Model.remove (the static method).

If you want to remove your docs one at a time (so that their middleware is invoked), then you do it using the instance method like you're currently doing.

If you want to remove all the matching docs in one go (bypassing any middleware), then you can use the static method and include the conditions in the call:

Message.remove({$or: [{'from.id': req.user.id}, {'to.id': req.user.id}]})
       .exec(function(err){...})
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471