Search code examples
meteormeteor-autoform

How to retrieve and group by Week my meteor mongo data?


I would like to retrieve my data and group them by week ?

For example:

12/09 - Data1  
12/09 - Data2  
13/09 - Data3    
26/09 - Data4 

and would like to have them like that:

from 12/09 to 19/09     
 1. Data1  
 2. Data2
 3. Data3

from 20/09 to 27/09  
 1. Data4

both/collections/hours.js

    Hours = new Mongo.Collection("hours");

    Hours.attachSchema(new SimpleSchema({
      createdBy: {
        type: String,
        autoValue: function() {
          return Meteor.userId()
        }
      },

//Here's the Day Date I want to retrieve in a Weekly Group

date: {
    type: Date,
    label: "Date",
    min: new Date("2014-01-01T00:00:00.000Z"),
    autoform: {
      value: new Date("2014-10-18T00:00:00.000Z")
    }
  }
}));

}

client/views/hours.js

// List Hours
Template.showHours.helpers({
     hours: function() {
        return ReactiveMethod.call('groupedByWeek');
      }
 });

server/server.js

if (Meteor.isServer) {
    Meteor.methods({
        groupedByWeek: function() {
            var query = [{
                $group: {
                    _id: {
                        $week: "$date"
                    },
                    Data: {
                        $push: "$$ROOT"
                    }
                }
            }]; // This Query will return data grouped by week number. date is the field with date

            return Hours.Data.aggregate(query); // Use your Collection here
        }
    });

views/hours/hours.html

 {{#each hours}}
    <tr>
                    <td valign="middle">{{formatDateHours date}}</td>
                    <td valign="middle">{{workinghour}}h</td>
                    <td valign="middle">{{class}}</td>
                    <td valign="middle">{{project}}</td>
                    <td valign="middle">{{summary}}</td>
                    <td valign="middle" align="right">
                        <div class="table-icon-buttons">
                            <i class="fa fa-pencil-square-o edit"></i>
                            <i class="fa fa-times delete"></i>
                        </div>
                    </td>
    </tr>
 {{/each}}

Solution

  • First Add meteorhacks:aggregate and simple:reactive-method to your project.

    Meteor Allows Aggregation Function only on the server side.

    so you will have to write a Meteor Method as Follows

    Meteor.methods({
        groupedByWeek: function () {
    
            var query = [
                {$group: {_id: { $week: "$date" }, Data: {$push: "$$ROOT"}}}
            ]; // This Query will return data grouped by week number. date is the field with date
    
            return Collections.Data.aggregate(query); // Use your Collection here
        }
    })
    

    Now Inside your Helper call

    return ReactiveMethod.call('groupedByWeek');

    You have to use reactive method otherwise it will return null as Method.call is async

    Hope this Helps !!