Nested documents and Blaze


#1
    {
            "_id" : "gteXreonYrQLToFNb",
            "team" : "Pittsburg",
            "League" : "NHL",
            "team_id" : "duuA4Fh9xrg4HTm55",
            "player_name" : "Crosby Sidney",
            "player_position" : "Forward",
            "games" : [
                    {
                            "match_id" : "m5beht7xLd3RvgRck",
                            "division" : "Regular Season",
                            "attending" : true
                    },
                    {
                            "match_id" : "m5beht7xLd3JvWRck",
                            "division" : "Regular Season",
                            "attending" : true
                    },
                    {
                            "match_id" : "m5be2t7xLd3JvgRck",
                            "division" : "Regular Season",
                            "attending" : flase
                    },
                    {
                            "match_id" : "m3beht7xLd3JvgRck",
                            "division" : "Regular Season",
                            "attending" : true
                    }
            ]
    }
{
        "_id" : "9mGSaqzHs6Wi9TR2B",
        "team" : "Pittsburg",
        "league" : "NHL",
        "team_id" : "duuA4Fh9xrg4HTm55",
        "player_name" : "Evgeni Malkin",
        "player_position" : "Forward",
        "games" : [
                {
                        "match_id" : "7KdC65n3SWoCTukhN",
                        "division" : "Regular Season",
                        "attending" : false
                }
           
        ]
}

The documents above are from MongoDB. I wondering how could I send this to Spacebards with just single helper, so the outcome would be pretty like this:

<table>
{{#each player_collection}}}
<tr><td>{{player_name}}</td><td>{{#each games}} {{match_id}} {{/each}}</td></tr>
{{/each}}
</table>

#2

I would do this when I wasn’t using an “outter #each

<tr><td>{{player_collection.player_name}}</td><td>{{#each player_collection.games}} {{this.match_id}} {{/each}}</td>

But I can’t remember if you can use #each inside #each

You may want to make template for each game…then you can use #each game and match_id it will take the data from the outter template

<table>
{{#each player_collection}}}
<tr><td>{{player_name}}</td>{{> EachGameTemplate}}</tr>
{{/each}}
</table>

#3

@a.com Thanks worked! Another thing I was wondering was how can I count where match_id = “m5beht7xLd3RvgRck” and push that to spacebars. Do I need to use JavaScript or can I some how use count(). ?


#4

I’m not sure I follow.

are you looking for a count of the number of “matches” in that array of games?


#5

Yes that is what I was looking for. However as Meteor is missing live aggregations etc. functions I started to create my own JavaScript functions to process my data at client-side, and it seems to be working like a charm


#6

Yeah I would just store the array in a variable and do a .length on it. Use that as the count.