55 so.....for some reason it seems that mongoose removes all the values from the object you pass to the save function that are not in the schema (which makes sense) BEFORE your middle-ware is called (Which is the most retarded thing ever).
explanations as to why it annoys me to say the least.
lets say I want to store an int in my mongodb you get something like
numbers.save({someNumber :1})
now, lets say that what I actually want to store is this number divided by 2 (and doing it every time I get the number out of the database is not an option)
I would make a small function as middleware
Code:
numbersModel.pre("save",function(next){
this.someNumber=this.inputNumber/2;
this.inputNumber=null
next()
})
//back in the controller
numbers.save({inputNumber:2})
except that this doesn't work as the database schema doesn't have a field inputNumber thus it gets removed before your middle ware gets called.
Sure, I could reuse someNumber but I find that both ugly and I doubt it would work if the type of value you store is different then the one you pass it.