0

I have a database related to the interview process which consists of multiple fields. basically, there are 5 APIs

  1. POST - (Candidate info) - for entering candidate data
  2. PATCH - for updating candidate info
  3. PATCH - (for Short Listing and reviewing) - updates in existing candidate
  4. PATCH - (for Scheduling the candidate interview) - entering the interview field which is an array object.
  5. GET Method

I want auto increment for the count field under Interview Round Count whenever the PATCH Method is updated for the same candidate (data updated successfully) How can I do that in Mongodb

Complete Schema:

    const mongoose = require('mongoose');
    const mongoosePaginate = require('mongoose-paginate-v2');
    const { Schema } = mongoose;
    const { EMAIL } = require('../../config/patterns.js');
    
    const InterviewSchema = new Schema(
      {
        firstName: {
          type: String,
          required: true,
          trim: true,
          maxlength: 30,
        },
        lastName: {
          type: String,
          trim: true,
          maxlength: 30,
        },
        email: {
          type: String,
          required: true,
          trim: true,
          match: EMAIL,
        },
        gender: {
          type: String,
          required: true,
          enum: ['male', 'female', 'other'],
        },
        contactNumber: {
          type: Number,
          unique: true,
          required: true,
        },
        alternateContactNumber: {
          type: Number,
        },
        resume: {
          type: String,
          required: true,
        },
        designation: {
          type: String,
          required: true,
          enum: ['trainee', 'se', 'sse', 'tl', 'systemEngineer'],
        },
        profile: {
          type: String,
          required: true,
          enum: [
            'react',
            'reactNative',
            'node',
            'fullstack',
            'php',
            'ios',
            'android',
            'python',
          ],
        },
        experience: {
          years: {
            type: Number,
            required: true,
          },
          months: {
            type: Number,
            required: true,
          },
        },
        ctc: {
          current: {
            type: Number,
            required: [true, 'In LPA'],
          },
          expected: {
            type: Number,
            required: [true, 'In LPA'],
          },
          offered: {
            type: Number,
            required: [true, 'In LPA'],
          },
        },
        noticePeriod: {
          type: Number,
          default: 0,
        },
        referrer: {
          type: {
            type: String,
            enum: ['consultant', 'employee', 'website', 'social'],
          },
          name: {
            type: String,
            trim: true,
            required: true,
          },
        },
        status: {
          type: String,
          enum: [
            'shortlisting',
            'shortlisted',
            'interviewing',
            'selected',
            'rejected',
            'onHold',
            'denied',
            'offerSent',
            'joined',
            'cancel',
          ],
        },

   

 // Shortling the Candidate - PATCH Method

    reviewer: {
      name: {
        type: String,
        trim: true,
        required: true,
      },
      email: {
        type: String,
        trim: true,
        required: true,
      },
      id: {
        type: Number,
        required: true,
      },
    },
    date: {
      type: Date,
      default: Date.now,
    },
    
// Scheduling the interview (this can be repeated no.of times for the interview round)
// represented in Array object

    interview: [
      {
        interviewerName: {
          type: String,
          trim: true,
        },
        date: {
          type: Date,
          default: Date.now,
        },
        mode: {
          type: String,
          enum: ['telephonic', 'video', 'f2f'],
          default: 'telephonic',
        },
        meeting: {
          link: {
            type: String,
            trim: true,
          },
          platform: {
            type: String,
            trim: true,
          },
        },

        round: {
          count: {         // want auto-increment count
            type: Number,
          },
          type: {
            type: String,
            enum: ['written', 'technical', 'hr'],
          },
        },

        interviewStatus: {
          type: String,
          enum: ['rejected', 'onHold', 'selected', 'schedule'],
        },

        feedback: {
          technical: {
            type: Number,
            required: true,
            min: 1,
            max: 5,
          },
          logical: {
            type: Number,
            required: true,
            min: 1,
            max: 5,
          },
          communication: {
            type: Number,
            required: true,
            min: 1,
            max: 5,
          },
          comment: {
            type: String,
            min: 10,
            max: 200,
          },
        },

        recommendation: {
          type: String,
          enum: ['yes', 'no'],
        },
      },
    ],
  },
  {
    timestamps: true,
  },
);

InterviewSchema.plugin(mongoosePaginate);
const InterviewProcess = mongoose.model('interviewprocess', InterviewSchema);
module.exports = InterviewProcess;
RK Varun
  • 61
  • 5
  • Does this answer your question? [Mongoose auto increment](https://stackoverflow.com/questions/28357965/mongoose-auto-increment) – Artem Arkhipov Dec 21 '22 at 13:05
  • @ArtemArkhipov No, I have applied the logic its just increment the either _id field or creates a new field with autoincrement. **I want to autoincrement inside the schema field for the round.count** – RK Varun Jan 06 '23 at 13:01

0 Answers0