Mongoose soft delete. I created it because other packages that I tested didn't work properly.
Model.remove()
was extended to set deleted: true
without real removing.
To methods find
, findOne
, update
, findOneAndUpdate
, findOneAndRemove
were added middleware to show only data without deleted: true
.
const mongoose = require('mongoose');
const softDelete = require('mongoose-soft');
const db = mongoose.createConnection('mongodb://localhost:27017/your-db');
const schema = new mongoose.Schema({ text: String });
schema.plugin(softDelete);
db.model('posts', schema);
If you want to see deleted data pass deleted: true
in the query object.
If you want to see all data pass deleted: { $in: [null, true, false] }
.