DEV Community

Discussion on: Build a Quiz REST API with NodeJS

Collapse
 
raynerray profile image
RaynerRay

Hi Carlos , another question , how can i create a relationship between the questions and a topic/subject, so that a user can select questions of a certain topic/subject only

Collapse
 
raymag profile image
Carlos Magno

Hey! Well, I see two ways of accomplishing that, one easy but not so good and another hard but a better solution.

The easy way is to just modify the Question model and add a "topic" field into it (it can either be a string or an array). The problem with that solution is that it can lead you to a lot of errors when registering new questions.

Now the second way is to create a model for the "topics" and modify the Question model with a new field for the "topics" (in this case, you would get the _id of the topic you want and save it inside the question). This way you can create new topics in the database and link them to the Questions.

It's kinda hard to explain how to do this with a comment, so I might write a new post to explain how you can build relationships between documents with mongodb as well. I hope I've been helpful.

Collapse
 
raynerray profile image
RaynerRay

true it's kinda hard to explain with a comment, maybe with code it'll be much clearer

Thread Thread
 
raynerray profile image
RaynerRay

I'm failing to direct you , want to show you my code, how can i dm you

Thread Thread
 
raymag profile image
Carlos Magno

I'm going to send you a message then.