Model.restore is broken when using use$neOperator = false
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported
Mongoose version
7.4.2
Node.js version
20.11
MongoDB server version
5.0.24
Typescript version (if applicable)
No response
Description
We have noticed that the Model.restore is removing the field deleted from the document.
This previously used to work.
We are using the use$neOperator with a value of false - in other words, all of our queries look for data where deleted = false as a result of https://github.com/dsanel/mongoose-delete/issues/50.
The following change, made in version 1.0.1 has broken this functionality:
- https://github.com/dsanel/mongoose-delete/commit/b8c3cea2321dcd583c78c254d1490b3f72c0c14f
Before this code change, the schema.statics.restore was:
schema.statics.restore = function (conditions, callback) {
if (typeof conditions === 'function') {
callback = conditions;
conditions = {};
}
var doc = {
deleted: false,
deletedAt: undefined,
deletedBy: undefined
};
return updateDocumentsByQuery(this, conditions, doc, callback);
};
After this code change, the function is:
schema.statics.restore = function (conditions, callback) {
if (typeof conditions === 'function') {
callback = conditions;
conditions = {};
}
var doc = {
$unset:{
deleted: true,
deletedAt: true,
deletedBy: true
}
};
return updateDocumentsByQuery(this, conditions, doc, callback);
};
The issue here, is that the deleted key is being removed from the model, which doesn't support how the use$neOperator works!
I have tested the following which appears to work - could this be considered for the 7.6.x release please?
schema.statics.restore = function (conditions, callback) {
if (typeof conditions === 'function') {
callback = conditions;
conditions = {};
}
var doc = use$neOperator ? {
$unset: {
deleted: true,
deletedAt: true,
deletedBy: true
}
} : {
deleted: false,
$unset: {
deletedAt: true,
deletedBy: true
}
};
return updateDocumentsByQuery(this, conditions, doc, callback);
};
Steps to Reproduce
Configure the mongoose-delete plugin as follows:
import softDelete from 'mongoose-delete';
export const softDeletePlugin = [
softDelete,
{
deletedAt: true,
deletedBy: true,
overrideMethods: true,
use$neOperator: false,
},
];
When you add this plugin to your schema, delete a document, and then try to restore it:
await Model.restore({ _id})
The deleted key has been removed from the underlying document.
Expected Behavior
The deleted key should remain in the document, and have a value of false
This GitHub repo is for mongoose, not mongoose-delete, so it would likely be better to report this as a bug on the mongoose-delete GitHub repo.
That being said, this does look like an unintentional bug introduced in mongoose-delete. We will see if we can open a PR to fix
This GitHub repo is for mongoose, not mongoose-delete, so it would likely be better to report this as a bug on the mongoose-delete GitHub repo.
That being said, this does look like an unintentional bug introduced in mongoose-delete. We will see if we can open a PR to fix
My apologies @vkarpov15 ... silly mistake! Thank you so much!
I think this issue can be closed since it indeed relates to mongoose-delete. This issue has also been addressed in a PR that's pending a review and merge.
I prefer to keep this open until the mongoose-delete PR is merged so we can remember to follow up on it