I am creating a conditional questionnaire where each user need to give answer few question to sign up. Each next question will appear based on the previous question answer submitted.
So I create schema for questions and answer.
Question Schema:
_id: Object Id,
name: String,
text: String,
hint: String,
skip: boolean,
type: String, // boolean, multiple, dropdown etc
children: Array //Ojbect Ids of child question
Answer Schema:
_id: Object Id,
question_id: Reference Id,
next_question: Reference Id,
text: String,
isBool: boolean
So, I don't think you need children since the next question will be determined by next_question
There are two types of question: 1. Normal, 2. Child. So if any user selects a question which has children, I need to show the child questions on the same page. And if a question doesn't have any children I will send them to next question on a new screen.
Children will show based on answer.
For example Q: Do you have any car? If the user clicks on yes, show them child questions which may ask information of car. But if the user selects no, We don't need to ask any more child question and I will redirect to next page which have new question.
In simple line: Children question will appear on same page, normal question will come always on new page.
Your question is kind of broad. And I don't know if I understood correctly, but here's a try.
First, I would modify your design a little bit.
We know that each question has a bunch of answers. I would consider that one-to-many relationship. In MongoDB, there are two recommended ways of modelling that relationship: using embedded documents or references. I think the latter would be an ideal choice in your case. The benefit of this approach is that you'll be able to use populate() to easily get all your related answers for each question. Otherwise, your current approach might require that you use some aggregation/lookup.
Now, each answer determines which question to go to, correct? So, I don't think you need children since the next question will be determined by next_question.
Here's how I would do it:
Question Schema
_id: Object Id,
name: String,
text: String,
hint: String,
skip: boolean,
type: String,
// remove children
answers: Array of Reference Ids
Answer Schema
_id: Object Id,
// remove question_id
next_question: Reference Id,
text: String,
isBool: boolean
Q1: Admin can add/edit/delete the question so is there any update require to this schema?
If you are asking whether you need to make any changes to the schema with the requirement that admin will be managing the questions? I would say no.
Q2: How can I manage nested question on same page ie how front-end will know next question will show on same page or next page? If I add one more field will be enough?
Uh... If you are asking how to have each question remain on the same page without any reloading? then you are talking about possibly AJAX.
In your Node app, I would set up a route for getting the "first" question with its related answers and a route for getting a "specific" question by its ID with its related answers.
You would build your question page starting with the "first" route. When the user clicks on an answer or something, you would make an AJAX call to the "specific" route passing the related next_question as parameter. Use the response from the call to rebuild the part of the page.
However, working with AJAX and DOM manipulation which are both whole other questions of its own.
Q3: How to avoid the question loop ie Q1->Q2->Q3->Q1.
I am assuming that the page for administering each question with its related answers will have a mechanism to set the next_question for each answer. The admin can simply ensure that the next_questions are in order.