Friday 5 May 2017

How do I access document object in pre update mongoose middle-ware plugin?

consider, I have a mongoose schema like this

const mySchema = new mongoose.schema({
val1:Number,
val2:Number
val_1_isBigger:Boolean})

Now I want to compare both the val1 & val2 before every update opearation and accordingly I wanna set Boolean attribute for val_1_isBigger.

My question is how do I access model object during pre('update') mongoose plugin opeation. Look at following example

mySChema.plugin(function(schema, options) {
    schema.pre('update', function(next) {

//Here How Do I compare val1 & val2 before update happenns
//and then set value here accordingly

  this.update({}, { $set: { val_1_isBigger: true/false } });

}
}



via Point Networks

No comments:

Post a Comment