Mongoose: Count removed documents
I tried this with latest version of mongoose, and it did not work. As the second parameter comes back as operation result, not just count. Used as below, it worked :
Model.remove({
myId: req.myId
}, function(err, removeResult) {
if (err) {
console.log(err);
}
if (removeResult.result.n == 0) {
console.log("Record not found");
}
Console.log("Deleted successfully.");
});
Mongoose < 4, MongoDB < 3
The second parameter to the remove
callback is a number containing the number of documents removed.
MyModel.remove({_id: myId}, function(err, numberRemoved) {
if(numberRemoved === 0) next(new Error("ID was not found."));
}
Mongoose 4.x, MongoDB 3.x
The second parameter passed to the remove
callback is now an object with the result.n
field indicating the count of removed documents:
MyModel.remove({_id: myId}, function(err, obj) {
if(obj.result.n === 0) next(new Error("ID was not found."));
}