I have this function that will update my task collection. The problem is how can I add new value into the array of a specific attachment using $push.
Schema
const taskSchema = mongoose.Schema({
projectId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'projectModel',
},
attachments: [
{
fileThumbnail: { type: String },
name: { type: String },
size: { type: Number },
uploadDate: { type: String },
status: {
type: Boolean,
default: true,
},
},
],
});
taskController
export const updateTaskById = async (req, res) => {
try {
const {
_id,
projectId,
attachments,
} = req.body;
const task = await taskModel.findById(req.params.id);
if (task) {
task.invitedPeopleId = invitedPeopleId || task.invitedPeopleId;
}
await task.save({
validateBeforeSave: false,
});
} catch (error) {
console.log(error.message);
}
};
Solution I tried
taskModel.updateOne(
{ projectId: projectId },
{ $push: { attachments: attachments } },
);