Mongoose Soft Remove
Mongoose Soft Remove is a Mongoose plugin that transparently introduces soft deletion to mongoose. It does this by overriding built in model methods. Note that these changed do not break compatibility with the Mongoose API; all changes are a superset of existing functionality. Because of this you can simply drop in this plugin and have soft delete functionality without any code changes.
When you call the static or instance remove method on a model, instead of deleting the document, the document is marked as removed.
Contact;// orContact;
If you wish to actually delete the document instead of soft deletion you may do
so by setting softRemove
to false
in the options object for the remove
instance method, or by setting $softRemove
to false
in the query object
passed to the static remove method.
Contact;// orContact;
Once a document has been soft deleted it will not be found by count, update,
find, findOne, findOneAndRemove, findOneAndUpdate, or findById unless
explicitly requested by setting isRemoved
to true
in the query object.
Contact;Contact;
The library also provides a static and instance method for restoring documents.
Contact;// orContact;
Things to note
This plugin adds a removedAt
and a virtual isRemoved
property to each schema
affected. removedAt
is indexed. removedAt
is only set on the document if
the document is soft deleted.