我有简单的猫鼬模型:

var ExampleSchema = new mongoose.Schema({
  fullHeight: {
    type: Number
  },
  partHeight: {
    type: Number
  }
});


是否可以从fullHeight设置partHeight参数的依赖关系?所需语法的示例在这里:

var ExampleSchema = new mongoose.Schema({
  fullHeight: {
    type: Number
  },
  partHeight: {
    type: Number,
    default: fullHeight / 2
  }
});

最佳答案

否,但是您可以设置一个pre-save middleware,每次保存时都执行此操作

ExampleSchema.pre('save', function(next) {
    this.partHeight = this.fullHeight / 2;
    next();
});

10-06 08:08