How to update and Save Array object in MongoDB (Mongoose + MongoDB)

100 Views Asked by At

I want to save the interview array object. I want when scheduled round 1 occurs and their feedback too based on round 1, want to save round 1 data with feedback and round 2 data with feedback, and so on

interview array has two objects

  1. schedule (for scheduling the number of rounds)
  2. feedback (for getting the feedback of every rounds occur)

Here is my Mongoose Schema

const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');
const { Schema } = mongoose;

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,
    },
    gender: {
      type: String,
      required: true,
    },
    contactNumber: {
      type: Number,
      required: true,
    },
    profile: {
      type: String,
      required: true,
    },
    status: {
      type: String,
    },

  **// Interview Array Objects, Need to update and save**

    interview: [
      {
        schedule: {
          interviewerName: {
            type: String,
            trim: true,
          },
          date: {
            type: Date,
            default: Date.now,
          },
          mode: {
            type: String,
          },
          meeting: {
            link: {
              type: String,
              trim: true,
            },
            platform: {
              type: String,
              trim: true,
            },
          },
          round: {
            count: {
              type: Number,
              default: 1,
            },
            type: {
              type: String,
              default: 'hr',
            },
          },
        },

        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,
            default: '',
          },

          interviewStatus: {
            type: String,
          },
          recommendation: {
            type: String,
            default: '',
          },**
        },
      },
    ],


  },
  {
    timestamps: true,
  },
);

InterviewSchema.plugin(mongoosePaginate);
const InterviewProcess = mongoose.model('interviewprocess', InterviewSchema);
module.exports = InterviewProcess;

I have done the update part i.e in the array object schedule round is updated with feedback but I am unable to save the data, it just overrides the previous schedule.

Here is code what I have done:

static async updateSchedule(request, h) {
    try {
      const payload = request.payload.interview[0];
      const params = request.query._id;
      const interviewSchedule =
        await interviewProcessService.findByinterviewProcessId(params);
      if (!interviewSchedule) {
        return h.response({
          statusCode: MESSAGE.STATUS.ERROR,
          message: MESSAGE.ID_NOT_EXIST,
        });
      }

      let mode,
        meeting,
        round,
        technical,
        logical,
        communication,
        comment,
        interviewStatus,
        recommendation,
        interviewerName;
      if (payload.schedule) {
        interviewerName = _.get(payload.schedule, 'interviewerName');
        mode = _.get(payload.schedule, 'mode');
        meeting = _.get(payload.schedule, 'meeting');
        round = _.get(payload.schedule, 'round') || {};
      }

      if (payload.feedback) {
        mode = _.get(interviewSchedule.interview[0].schedule, 'mode');
        meeting = _.get(interviewSchedule.interview[0].schedule, 'meeting');
        round = _.get(interviewSchedule.interview[0].schedule, 'round') || {};
        interviewerName = _.get(
          interviewSchedule.interview[0].schedule,
          'interviewerName',
        );

        technical = _.get(payload.feedback, 'technical');
        logical = _.get(payload.feedback, 'logical');
        communication = _.get(payload.feedback, 'communication');
        comment = _.get(payload.feedback, 'comment');
        interviewStatus = _.get(payload.feedback, 'interviewStatus');
        recommendation = _.get(payload.feedback, 'recommendation');
      }

      const result = await interviewProcessService.update(params, {
        interview: {
          schedule: {
            interviewerName,
            mode,
            meeting,
            round,
          },
          feedback: {
            technical,
            logical,
            communication,
            comment,
            interviewStatus,
            recommendation,
          },
        },
      });

      return h.response({
        result,
        statusCode: MESSAGE.STATUS.SUCCESS,
        message: MESSAGE.ROUTES.SCHEDULE_UPDATE.MESSAGE,
      });
    } catch (error) {
      return ErrorHandler.error(error);
    }
  }

Want to Achieve: whenever the GET method is applied, I want to receive no. of rounds with feedback.

1

There are 1 best solutions below

2
Alisha Vashisht On

var update = {name: 'updated item2', value: 'two updated'}; Person. update({'items.id': 2}, {'$set': {'items. $': update}}, function(err) { ... Problem with this approach is that it updates/sets the entire object, therefore in this case I lose the id field.