Conditionally skip mongoose hook function

Viewed 1817

I have a pre-save hook to encrypt password field of User schema, like:

var schema = new mongoose.Schema({
    username: 'string',
    password: 'string'
});

schema.pre('save', encrptPasswordHook);
schema.pre('update', encrptPasswordHook);
schema.pre('findOneAndUpdate', encrptPasswordHook);
...

By this way, I have encrypted password string in my database every time a User created or updated.

Now I have a JSON file of old User data with the encrypted password. I want to use this User model to import the JSON file into my database.

How can avoid the pre-save hook to encrypt the password again?

1 Answers
Related