mongoose get db value in pre-save hook
mongoose, node.js
Solution
By default, the old values are not stored. You would have to do is track the old data with a post init hook (a mongoose feature).
What we do is attach copy of the original document to all items pulled from MongoDB. We have this code for each schema we need to get pre-dirty data for comparison:
schema.post( 'init', function() {
this._original = this.toObject();
} );
NodeJS is pretty efficient, and does copy on write when possible, so you don't see double the memory consumption unless you modify the entire document. Only then does _original actually consume double the memory.
Problem
I want to know what the 'clean' value of a dirty prop is in a pre-save mongoose hook like this: ``` UserSchema.pre('save', function(next) { var user = this; if (user.isModified('password')){ //i want to know what the value of user.password was before it was changed } next() } ``` Is it possible to look up the old value without looking it up in the db?