Skip to content

Commit

Permalink
fix(query): set deletedCount on result of remove()
Browse files Browse the repository at this point in the history
Fix #7629
  • Loading branch information
vkarpov15 committed Apr 7, 2019
1 parent d101c68 commit 76d8ca5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,13 +1674,14 @@ Model.translateAliases = function translateAliases(fields) {
*
* ####Example:
*
* Character.remove({ name: 'Eddard Stark' }, function (err) {});
* const res = await Character.remove({ name: 'Eddard Stark' });
* res.deletedCount; // Number of documents removed
*
* ####Note:
*
* This method sends a remove command directly to MongoDB, no Mongoose documents
* are involved. Because no Mongoose documents are involved, _no middleware
* (hooks) are executed_.
* are involved. Because no Mongoose documents are involved, Mongoose does
* not execute [document middleware](/docs/middleware.html#types-of-middleware).
*
* @param {Object} conditions
* @param {Function} [callback]
Expand Down
10 changes: 8 additions & 2 deletions lib/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,13 @@ exports.handleDeleteWriteOpResult = function handleDeleteWriteOpResult(callback)
if (error) {
return callback(error);
}
return callback(null,
Object.assign({}, res.result, {deletedCount: res.deletedCount }));
const mongooseResult = Object.assign({}, res.result);
if (get(res, 'result.n', null) != null) {
mongooseResult.deletedCount = res.result.n;
}
if (res.deletedCount != null) {
mongooseResult.deletedCount = res.deletedCount;
}
return callback(null, mongooseResult);
};
};

0 comments on commit 76d8ca5

Please sign in to comment.