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.
update(), so it probably does not match anything due to being fed incorrect parameters. Also you are probably using "mongoose". If so then show the schema so we can see ifquestionIdis properly defined as anObjectIdtype.ObjectIdneed to be cast as such manually since any values in thereq.bodyare just going to be "strings". And "duplicate 2" shows additional and alternate syntax which would not be necessary to "append" but is necessary for update. There is also information there on why you probably should avoid "nesting arrays" where possible. People always "nest" for "relational" reasons to make things easy. It actually makes things worse. It's a very common misconception.