Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"group by" queries on meteor collection

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

like image 285
shihandrana Avatar asked Feb 11 '14 04:02

shihandrana


2 Answers

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});
});
like image 69
David Weldon Avatar answered Nov 09 '22 04:11

David Weldon


You can user those functions with meteor, you are talking about, or found a link to what describes the client side processing, but from the server side it is different.

You should be using aggregate rather than the mapReduce or group functions for most cases. The whole pipeline is implemented in C++ rather than passing onto the JavaScript engine. In all cases the equivalent functions in aggregation pipeline will be faster.

> db.dates.aggregate([
    {$group: { _id: {$dayOfWeek: "$CreatedDate"}, Total: {$sum: 1} }},
    {$project: { _id: 0, day: "$_id", Total: 1 } },
    {$sort: { day: 1 }}
  ])


  {
    "result" : [
            {
                    "Total" : 1,
                    "day" : 4
            },
            {
                    "Total" : 1,
                    "day" : 6
            },
            {
                    "Total" : 2,
                    "day" : 7
            }
    ],
    "ok" : 1
  }

About Meteor Support

Server side, meteor uses the node-mongodb-native driver for all processing. It is maintained by MongoDB and maintains all features.

Client side is a library known as "mini-mongo". This is not actually a driver but a little layer that is actually using sockets connections to pass details through to the server. It makes your client side javascript "look" the same as server side code, but you are not actually talking to mongo directly.

There is nothing wrong with using server side functions that are "published" to the client to invoke your custom code on the server side. As said, it's just the standard driver you can use in other nodejs applications.

like image 23
Neil Lunn Avatar answered Nov 09 '22 03:11

Neil Lunn