A query problem for someone to solve

My software is setup like a book of questions

  • book is made of pages
  • page is made of question_groups
  • question_groups are made of questions

Book, Page, Question_Group, and Question are all different collections

At a new part in my software, I have a new requirement:

  • given a certain question, I need to know all of the questions that follow

Here is my helper but it’s not working. Can someone help?

` …

questions_after_this': function(question_id) {
      var q = Question.findOne({_id: question_id}); 
      var results = [];
      var group = Question_Groups.findOne({_id: q.group_id})
      while(group) {
         var questions = Question.find({group_id: q.group_id, sort_order: {$gt: q.sort_order}}, {sort: {sort_order: 1}});             
         results.concat(questions);
         group = Question_Groups.findOne({page_id: group.page_id, sort_order: (group.sort_order+1)}, {sort: {sort_order: 1}});
      }
  return results;
}

`
I apologize I don’t really understand how to insert code here.

Your code looks fine as long as it is readable, so don’t worry.

For the future, to insert code, either paste it, mark and use preformatted text button or use tripple grave accent in a line before the code and in a line after the code.

As for the question, is there a particular reason for which your company doesn’t denormalize such data? Perhaps this data structure was migrated from another database?

If you don’t want to denormalize, I can suggest using publish-composite package which can help you to publish related data like yours.

Structure: Before we continue, can you give us the record structure (fields) of the collections ala Simple Schema?

Scope: And which questions do you want? You wrote, “given a certain question, I need to know all of the questions that follow”.
Follow in what, in the group? the page? the book?

Structure:

Schema.Question = new SimpleSchema({
  group_id: { type: String },
  sort_order: { type: Number },
  text: { type: String },
  ...
)};

Schema.Question_Group = new SimpleSchema({
   page_id: { type: String },
   sort_order: { type: Number },
   ...
)};

Schema.Page = new SimpleSchema({
   sort_order: { type: Number },
   ...
)};

Scope:
In my UI, I start with a list of all of the questions on a page (each question is a collapsible header). For each question (in the collapsible-body) I want the user to be able to select a future question (as part of a form).

{{#each groups}}
  {{#each questions _id}}
     {{text}} ...
     <select>
       <option value="">Select a question to skip</option>
     {{#each question_after_this _id}}
       <option value="{{_id}}">{{text}}</option>
     {{/each}}
     </select>
  {{/each}
{{/each}}

brajt,

I am new to document databases, and you are likely seeing the influence of my relational database history.

I have posting some of the schema for Questions, Question_Groups and Pages here. I’m interested, how do you think I could be more efficient? I welcome any tutelage you may offer.

Check out reywood:publish-composite