Aggregate or not?


#1

Hello.
I have the collection “menu” with structure:
{
"_id" : “vJQr7EuieDHMuX2nx” “section” : “vine” “price” : “200” “category” : “bar”
"_id" : “vJQr7EuieDHMuX4rf” “section” : “beer” “price” : “200” “category” : “kitchen”
"_id" : “vJQr7EuieDHMuX5tg” “section” : “milk” “price” : “200” “category” : “kbar”
"_id" : “vJQr7EuieDHMuX4sc” “section” : “beer” “price” : “200” “category” : “kitchen”
"_id" : “vJQr7EuieDHMuX2xs” “section” : “vine” “price” : “200” “category” : “bar”
}

I want to show the user only “section” without repeating
For this I used
Menu.aggregate([{$group:{_id:"$section"}}])
but it’s not working
How can I do this? May be to use a different method?

Server side:
Meteor.methods({
  'getSections': function () {
    var pipeline = [{$group:{_id:"$section"}}];
    var result = Menu.aggregate(pipeline);
    var sections = [];
    for (i = 0; i < result.length; i++) {
      sections.push(result[i]._id);
    };
    console.log(sections);
    return sections;
  }
});

result in the server: [ ‘vine’, ‘beer’, ‘milk’ ]

Client side:
    Template.MobBarMenu.helpers({
        'list': function(){
            this.render('MobBarMenu', {});
        },
        'listSections': function () {
           Meteor.call('getSections', function (err, data) {
               console.log(data);
               // return data;
           });
        }
    });

template name="MobBarMenu"
            {{#each listSections}}
                {{this}}
            {{/each}}
template

I don’t know where to find the solution


#2

$aggregate is not reactive so you don’t want to use it anyway.

You’ll probably be better of doing something like this on the client using a library like underscore / lodash:

var menu =[
  {_id : "vJQr7EuieDHMuX2nx1", section : "wine", price : "200", category : "bar" },
  {_id : "vJQr7EuieDHMuX2ng2", section : "beer", price : "200", category : "bar" },
  {_id : "vJQr7EuieDHMuX2nx3", section : "milk", price : "200", category : "bar" },
  {_id : "vJQr7EuieDHMuX2nx4", section : "beer", price : "200", category : "bar" },
  {_id : "vJQr7EuieDHMuX2nx4", section : "wine", price : "200", category : "bar" }
];

var section_array = _.map(menu, function(item) { return item.section; });
var unique_sections_array = _.uniq(section_array); 

And you then plonk unique_sections_array into your helper.


#3

Thank you so match!!!