How to get array list as values


#1

i’m having issues on how to get the list of values from an array one by one dynamic. To pass to aggregate function Collection is like below

{
    "_id" : "gBF5yZxHQvfwsnoSJ",
    "title" : "Testing",
    "status" : "Open",
    "createdAt" : ISODate("2017-11-01T06:50:49.626Z"),
    "createdBy" : "user",
    "questions" : [ 
        {
            "qid" : "jBP9SbXFgAnHgwWhX",
            "question" : "Are you a Boy or a Girl",
            "type" : "radio",
            "options" : [ 
                {
                    "option" : "Boy",
                    "qid" : "jBP9SbXFgAnHgwWhX"
                }, 
                {
                    "option" : "Girl",
                    "qid" : "jBP9SbXFgAnHgwWhX"
                }
            ]
        }, 
        {
            "qid" : "hMDqLSB9QaTuCsNm2",
            "question" : "Up or Down",
            "type" : "radio",
            "options" : [ 
                {
                    "option" : "Up",
                    "qid" : "hMDqLSB9QaTuCsNm2"
                }, 
                {
                    "option" : "Down",
                    "qid" : "hMDqLSB9QaTuCsNm2"
                }
            ]
        }
    ]
}

i want to get “qid” : “hMDqLSB9QaTuCsNm2” and “option” : “Boy” for example… i can do aggregate by specific data or static but i want it to be dynamic qid and option is different always…

Template.result.helpers({
  questionList:function(){
    var controller = Router.current();
    questionId = controller.params._id;
    var a = QuestionCollection.find({_id: questionId});
    var qid = //??????????
    var option = //??????????
//Mateor.call aggregate function here qid option
  },
});

#2

I’m having trouble understanding your question (sorry!).

What do you mean you want to “get” “qid” : “hMDqLSB9QaTuCsNm2” and “option” : “Boy”?

Are you looking for all of the documents in the collection that have 'questions.qid': 'hMDqLSB9QaTuCsNm2' and 'questions.options.option': 'Boy'?

I’m confused why you have identical qid in 3 places on each question.


#3

yeah… but when i try to do a.questions.qid its just throwing undefined


#4

I went through something similar recently. Schemas like this make everything more difficult and also slow down your ddp connection (more data gets updated more often).

You are better off to denormalize the data into multiple collections instead of keeping everything nested like this. Do you know what that means? If not I will explain in more detail.


#5

already figured it out… using nested for loop…