首页 > 解决方案 > 如何告诉猫鼬在特定时间后自动增加存储值?

问题描述

假设我是这样的学生模型:

const Schema = mongoose.Schema;

const StudentSchema = new Schema({
    fullName: {
        type: String,
        required: true,
        unique: true,
        trim: true,
        maxLength: 60,
        minLength: 3,
    },
    studentID: {
      type: String,
      required: true,
      unique: true,
      trim: true,
      maxLength: 60,
      minLength: 3,
    },
    semester: {
      type: Number,
      min: 1,
      max: 15,
    }
});

module.exports = mongoose.model('StudentModel', StudentSchema);

如何告诉猫鼬在 Express 应用程序中每 6 个月自动增加一个学期?

标签: node.jsmongodbexpressmongoose

解决方案


这是一个例子可能会有所帮助。添加此属性。

semester_next_time: {
      type: Date,
    }

StudentSchema.pre('find', function() {
   // if semester_next_time is   less than now  do stuff and update next time semester
});

推荐阅读