I apologize for my poor way of explaining my issue. I am trying to update my schema's notes and assignmentHistory fields. I have no issues updating notes field but with the assignmentHistory field what I am trying to do is append values to the array only if the assingedTo value has changed from it's last value or is not empty only then $push to assignmentHistory array, otherwise don't add anything to assigmentHistory array. Basically, how do I prevent $push not add anything to the assignmentHistory array, not even empty object with _id field? Below is snippet of my code
const RequestSchema = new Schema(
{
title: { type: String, required: true, max: 32, min: 6 },
priority: { type: String, emum: ["low", "medium", "high"] },
notes: [
{
commentNotes: {
comment: { type: String, max: 200 },
commentBy: { type: Schema.Types.ObjectId, ref: "User" },
commentDate: { type: Date },
}
]
assignedTo: { type: Schema.Types.ObjectId, ref: "User" },
assigmentHistory: [
{
techName: { type: Schema.Types.ObjectId, ref: "User" },
assignedDate: { type: Date },
}
]
}
let noteObj={};
let techobj={};
await RequestModel.findOneAndUpdate(
{ _id },
{
$push: {
notes: [noteObj],
assignmentHistory:[techobj]
},
},
{ new: true }
);