I am developing an online questionnaire for students, each sections comprises of 4-5 different questionnaire each, and a student has the option of either retaking given questionnaire or make a new attempt on the same questionnaire.
The document schema is as follows
Document 1
_id: ObjectId('ID'),
userId: U01,
questionnaireId: Q01,
sectionId: section1,
completed: 1
Document 2
_id: ObjectId('ID'),
userId: U01,
questionnaireId: Q01,
sectionId: section1,
completed: 1
Document 3
_id: ObjectId('ID'),
userId: U01,
questionnaireId: Q02,
sectionId: section1,
completed: 1
Document 4
_id: ObjectId('ID'),
userId: U01,
questionnaireId: Q03,
sectionId: section1,
completed: 1
Document 5
_id: ObjectId('ID'),
userId: U01,
questionnaireId: Q01,
sectionId: section2,
completed: 1
Where completed represents that a user has attempted questionnaire, now as you can see that
questionnaireId: Q01,
sectionId: section1,
is being repeated twice with different _id, in this case it shall be counted as 1 instead of two, and the same process is followed for any combo of sectionId and questionnaireId.
The aggregation pipeline is as follows
{
$group: {
_id: {
originalId: '$_id',
userId: '$userId',
questionnaireId: '$questionnaireId',
sectionId: '$sectionId',
completed: '$completed',
},
completedQuestionnaire: { $sum: 1 },
},
},
Although I am getting percentage calculated in $project stage but I am not convinced whether this is the right approach or not
Any help will be appreciated