I have a document that comprises of the following schema (this collection is meant to save user’s history in completing a section). The following schema assumes that a section may have 1-M topics with each of their respective topics
{
"_id": "someId",
"history": [
{
"sectionId": "section0",
"topicId": 0,
"status": 1,
"completedDate": "2/9/2022 10:46:34 AM"
},
{
"sectionId": "section0",
"topicId": 1,
"status": 0
},
{
"sectionId": "section0",
"topicId": 2,
"status": 1,
"completedDate": "2/9/2022 8:24:35 AM"
},
{
"sectionId": "section0",
"topicId": 3,
"status": 0
},
{
"sectionId": "section0",
"topicId": 4,
"status": 0
},
{
"sectionId": "section1",
"topicId": 0,
"status": 0
},
{
"sectionId": "section1",
"topicId": 1,
"status": 0
},
{
"sectionId": "section2",
"topicId": 0,
"status": 0
},
{
"sectionId": "section2",
"topicId": 1,
"status": 0
}
]
}
In above example, status 0 refers to uncompleted topic within a section, whereas status 1 refers to completed topic with completedDate as well.
This document is generated dynamically after a user subscribes to an online quiz preparation (quiz-preparation collection) whose schema is like this
{
"_id": "someID",
"quizContents": [
{
"sectionId": "section0",
"topicName": "ABC",
"topics": [
{
"topicId": 0
},
{
"topicId": 1
},
{
"topicId": 2
},
{
"topicId": 3
},
{
"topicId": 4
}
]
},
{
"sectionId": "section1",
"topicName": "DEF",
"topics": [
{
"topicId": 0
},
{
"topicId": 1
}
]
},
{
"sectionId": "section2",
"topicName": "GHI",
"topics": [
{
"topicId": 0
},
{
"topicId": 1
}
]
}
]
}
Now suppose if quiz is updated (new topics or sections are added), or some sections are removed (suppose section 2), then user history shall reflect that as well, meaning if any section is deleted then it shall be deleted from user history (in first example) as well. Whereas if any section is updated with new topics then user history array shall have new topic as well with status set to 0.
Now when I update quiz sections, I am passing new array of objects to user history schema, (I am aware how to achieve this via JavaScript) but is there a way to achieve this using Mongodb Native Driver in Node.js