"group by" queries on meteor collection

aggregation-framework, meteor, mongodb

Solution

You'll need to group them manually. There are a number of ways to do that, but here's an (admittedly difficult to read) example:

var customers = Customer.find().fetch();

var groupedDates = _.groupBy(_.pluck(customers, 'CreatedDate'));

_.each(_.values(groupedDates), function(dates) {
  console.log({Date: dates[0], Total: dates.length});
});

Problem

My data `mongoDB`: ``` >db.CUSTOMER.find() {"Name": "A", "CreatedDate": "Wed Jan 29 2014"} {"Name": "B", "CreatedDate": "Fri Jan 31 2014"} {"Name": "C", "CreatedDate": "Sat Feb 01 2014"} {"Name": "D", "CreatedDate": "Sat Feb 01 2014"} ``` In meteor: ``` Customer = new Meteor.Collection("CUSTOMER"); ``` I'm trying to group them by date (Mon, Tues, Wed, ...) in meteor collection along with the total of the data. It should be something like this: ``` {"Date": "Wed Jan 29 2014", "Total" 1} {"Date": "Fri Jan 31 2014", "Total" 1} {"Date": "Sat Feb 01 2014", "Total" 2} ``` In mongoDB, I'd just go with http://docs.mongodb.org/manual/reference/method/db.collection.group/, but apparently, it is impossible in meteor, for it doesn't support findAndModify, upsert, aggregate functions, and map/reduce. Is there any examples of workaround that I can do to make it works? Thank you

Original source