首页 > 解决方案 > 如何暂时禁用猫鼬预钩

问题描述

在我的猫鼬模式中,我pre配置了一个非常有用的钩子来更新lastModified我的模型的时间戳。

var Document = new Schema({
  lastModified: {type: Date},
  title: {type: String, default: '', trim: true},
  ...
}
Document.pre('save', function (next) {
  this.lastModified = Date.now();
  next();
});

但是,当我们升级到新版本时,我需要修改文档的某些字段而不更改 lastModified。

有没有办法为某个save()调用或更好的设计停用 pre 挂钩,而不是使用 pre 进行时间戳更新?

标签: node.jsmongodbmongoosemongoose-schema

解决方案


推荐阅读