I work on a quiz application for my BD. I'm trying to update my MongoDB collection once the user correctly answers a question inside the Course collection (there will be multiple questions).
This is what I've tried so far - everyone seems to recommend this syntax, but it doesn't work for me. I am able to update everything outside the course object from the database.
router.put("/course", async (req, res) => {
await Course.update(
{ _id: req.body.courseId, "course.questionId": req.body.questionId },
{
$addToSet: {
"course.$.taken": [{ status: 1, attempts: 1, userId: req.body.userId }]
}
}
);
});
Once again - I am trying to append a new object to the "taken" array with the userId, status of the question (failed/passed), and the number of attempts.