diff --git a/lib/query.js b/lib/query.js index b40b0db58b6..190bf56c5f7 100644 --- a/lib/query.js +++ b/lib/query.js @@ -4301,6 +4301,7 @@ Query.prototype.orFail = function(err) { } break; case 'findOneAndDelete': + case 'findOneAndRemove': if (get(res, 'lastErrorObject.n') === 0) { throw _orFailError(err, this); } diff --git a/test/model.findOneAndRemove.test.js b/test/model.findOneAndRemove.test.js index d7a34b3b71b..5129d57d64c 100644 --- a/test/model.findOneAndRemove.test.js +++ b/test/model.findOneAndRemove.test.js @@ -347,6 +347,17 @@ describe('model: findOneAndRemove:', function() { }); }); + it('with orFail() (gh-9381)', function() { + const User = db.model('User', Schema({ name: String })); + + return User.findOneAndRemove({ name: 'not found' }).orFail(). + then(() => null, err => err). + then(err => { + assert.ok(err); + assert.equal(err.name, 'DocumentNotFoundError'); + }); + }); + describe('middleware', function() { it('works', function(done) { const s = new Schema({