Mongoose delete/pull ObjectID within an array

Written by Arvind

Slots

I wanted to delete an ID inside slots array, It took me a while to find the answer, hopefully this small snippet can help you.

// Delete item in users
User.findOneAndUpdate(
  { '_id': userID, 'username': username },
  { $pull: { slots: cardID }}, function(err, data) {
    if (err) res.render('error', { 
      message: 'Sorry failed to delete card id in users',
      error: { status: err, stacks: 'failed to delete card id in users' }
    });
  }
)

I’m using Express 4, Mongoose 5.2.7 and mongodb hosted in mlab.