// Delete a user by username
app.delete('/users/:Username', (req, res) => {
Users.findOneAndRemove({ Username: req.params.Username })
.then((user) => {
if (!user) {
res.status(400).send(req.params.Username + ' was not found');
} else {
res.status(200).send(req.params.Username + ' was deleted.');
}
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
Modelname.findOneAndDelete({ id: id })
Tank.deleteOne({ size: 'large' }, function (err) {
if (err) return handleError(err);
// deleted at most one tank document
});
Surf
.find({user_id: {$in: userIds}})
.populate('user_id', 'name boards') // added boards
.populate('friends', 'name')
// .populate('board_id', 'name size') // can't do this as discussed
.exec(function (err, surfs) {
if (err) {
return handleError(res, err);
}
surfs.forEach(function (surf) {
surf.set('boardInfo', surf.user_id.boards.id(surf.board_id), {strict: false});
});
// TODO: now remove the surf.user_id.boards.
return res.json(200, surfs);
});
No,
You cannot undo a delete operation.
Once deleted the data cannot be retrieved UNLESS a dump of the collections is created
before deleting it.
const res = await Character.remove({ name: 'Eddard Stark' });
res.deletedCount; // Number of documents removed