0

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);
 });
});
joshk132
  • 1,011
  • 12
  • 37
  • Can you list the collections as well as sample documents with which you want the user data removed? – chridam Mar 05 '18 at 21:27
  • 1
    @chridam Collections are AP Asset Assign Cables Checkin Checkout Custom DeskPhones Desktops Expendables Laptops Licence Man Mobile Monitor Printer Projector Router Server Subuser Switch Tablet I guess I was a bit off on the number of collections. 22 total plus the user one already in my code. As for sample data, it varries hugely between each model but each model has `author: { id: { type: mongoose.Schema.Types.ObjectId, ref: "User" }, email: String }` as part of it to tie it to each user. – joshk132 Mar 05 '18 at 21:34

0 Answers0