Armin E
Armin E

Reputation: 53

How to tell mongoose to automatically increase a stored value after a specific amount of time?

Let's say I a student model like this:

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);

How can I tell mongoose to automatically increase the semester by one each 6 months in an Express app?

Upvotes: 1

Views: 96

Answers (1)

Babak Abadkheir
Babak Abadkheir

Reputation: 2350

this is a example maybe could help. add this property.

semester_next_time: {
      type: Date,
    }

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

Upvotes: 1

Related Questions