emiljanitzek / mongoose-delete-ts

Mongoose Soft Delete Plugin
MIT License
10 stars 4 forks source link

feature: omit empty deleted attribute in hooks #25

Closed EDSprog closed 3 days ago

EDSprog commented 5 months ago

Hi! As a developer, I want to have the possibility to receive documents that have no "deleted" attribute by default as not deleted documents. Why do I need this feature? When I install the package first time, I don’t want to write additional migration to add deleted attribute to each entry in db. Thank you.

emiljanitzek commented 5 months ago

Thank you for you contribution. Unfortunately I will not be merging this pull request since this will have a negative impact on performance. If you have a good motivation for when using $ne is better, I might be open if you change your code to add this as a setting that can be activated in these cases. But generally you would be better of just migrating your data to add deleted: false to your documents. If you don't want deleted:false in your response you can add for a "private" plugin that removes the deleted property from your toJSON response (for example https://github.com/vs-archive/mongoose-private)

Mongodb explains it here (https://www.mongodb.com/docs/manual/reference/operator/query/ne/)

The inequality operator $ne is not very selective since it often matches a large portion of the index. As a result, in many cases, a $ne query with an index may perform no better than a $ne query that must scan all documents in a collection. See also Query Selectivity.