How can I check if the remove-method of a Mongoose model really has removed something?
MyModel.remove({_id: myId}, function(err, entry) {
if(entry == null) next(new Error("ID was not found.")); // this doesn't work
}
Can I check how many documents were removed?
In the Mongo-Documentation kristina1 write in a comment:
If you call db.runCommand({getLastError:1}) after a remove and the “n” field will tell you how many documents were deleted.
But I don’t know how to do this with Mongoose.
Mongoose < 4, MongoDB < 3
The second parameter to the
removecallback is a number containing the number of documents removed.Mongoose 4.x, MongoDB 3.x
The second parameter passed to the
removecallback is now an object with theresult.nfield indicating the count of removed documents: