mongodbmeteormeteor-collections

How to give each user a copy of documents in a central collection


I don't think I've given this a good title but I'm finding it hard to verbalise.

I'm building an application where there are some multiple choice questions that each user has access to.

How can I give each user access to their own "copy" of the multiple choice exam?

Right now what I'm doing is adding an entire copy of every question of all of my multiple choice questions to each user upon account creation, but this is going to get messy really quickly and means that in the future updating the questions or changing the exam would be a nightmare.

Is there a way that there can be one collection which houses the exam(s) and then each user has access to their own "copy" of that exam which records their answers etc?

Thanks


Solution

  • One way would be to set up your database to have a collection for exams and then each user's answers could be stored in a separate collection with their answers and a reference id to the exam and the user the answers belong to. Below is an example, which may be easier to understand.

    Exams collection sample data:

    {
      examId: 001,
      examType: "multiple-choice",
      examQuestions: [
        {
          questionNumber: 01,
          questionText: "How many collections should I use?",
          questionChoices: [
            {
              choice: "five",
              correct: 0
            }, 
            {
              choice: "three",
              correct: 0
            }
            {
              choice: "one",
              correct: 0
            }
            {
              choice: "two",
              correct: 1
            }
          ]
        },
        {
          questionNumber: 02,
          questionText: "What should I call my exams collection?",
          questionChoices: [
            {
              choice: "tests",
              correct: 0
            }, 
            {
              choice: "exams",
              correct: 1
            }
            {
              choice: "icecream",
              correct: 0
            }
            {
              choice: "bob",
              correct: 0
            }
          ]
        }
      ]
    }
    

    userAnswers collection sample data:

    {
      userId: "j23owdd9sJDO(Ee09kd",
      examId: 001,
      examAnswers: [
        {
          questionNumber: 01,
          questionAnswer: "two",
          correct: 1
        },
        {
          questionNumber: 02,
          questionAnswer: "bob",
          correct: 0
        }
      ]
    }
    

    This exam collection set up would give you the option to store multiple exams in your exams collection and track data like type of exam and give each exam a unique id for referencing it in your app or other collections. It would also keep your exam collection clean.

    This userAnswers collection setup would give you the option to store answers from multiple exams for each user. Storing whether the answer they gave was correct would allow you to more easily display their score for each exam and how many correct answers they had.