我有一个预存钩子来加密User模式的password字段,如下所示:
var schema = new mongoose.Schema({
username: 'string',
password: 'string'
});
schema.pre('save', encrptPasswordHook);
schema.pre('update', encrptPasswordHook);
schema.pre('findOneAndUpdate', encrptPasswordHook);
...通过这种方式,每次创建或更新User时,我都会在数据库中加密密码字符串。
现在我有了一个旧User数据的JSON文件,其中包含加密的密码。我想使用这个User模型将JSON文件导入到我的数据库中。
如何避免预存钩子再次加密密码?
发布于 2016-03-22 20:51:42
您可以使用User.collection.insert()绕过所有Mongoose验证(不会检查插入数据的类型)和挂钩,它直接使用MongoDB驱动程序:
var UserSchema = new mongoose.Schema({
username: 'string',
password: 'string'
});
var User = mongoose.model('User', UserSchema);
User.collection.insert({
username: 'Some Name',
password: 'The Encrypted Password'
});https://stackoverflow.com/questions/36153998
复制相似问题