I'm using mongoose-delete plugin.
I want to build a simple mongoose middleware so I add {deleted:false}
to every find
query on that schema.
var mongoose = require('mongoose'),
mongooseDelete = require('mongoose-delete'),
Schema = mongoose.Schema;
var MySchema = new Schema({
name: {type: String, required: true}
});
MySchema.plugin(mongooseDelete, {deletedAt: true, deletedBy: true});
MySchema.pre('find', function (next){
// I want to add {deleted: false} to the queries conditions
});
How should I implement the pre-find middleware?
In pre-find middleware, this
is the Query
object so you can add {deleted: false}
to the query using:
MySchema.pre('find', function() {
this.where({deleted: false});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With