I need to figure out how to delete all documents in a mongo database where the email is req.user.email
when I execute my code for deleting an account. Currently it just deletes the account and leaves the users old documents and is filling up my database with useless data. I am using node.js, express.js, mongodb and mongoose.js
I have many collections ~10 What is the best way to clean everything up when a user leaves.
My delete account code
exports.deleteAccount = function(req, res, next){
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
user.remove(function (err, user) {
if (err) return next(err);
user.cancelStripe(function(err){
if (err) return next(err);
req.logout();
req.flash('info', { msg: 'Your account has been deleted.' });
res.redirect(req.redirect.success);
});
});
});
};
I am thinking this might be it then do it for each collection?
Asset.find({"author.id":req.user.id}, (err, assetReturned)=>{
assetReturned.remove((err, asset) =>{
if (err) return next(err);
});
});