“分组依据"流星收集查询 [英] "group by" queries on meteor collection

查看:35
本文介绍了“分组依据"流星收集查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据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"}

在流星中:

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}

在 mongoDB 中,我会选择 http://docs.mongodb.org/manual/reference/method/db.collection.group/,但显然在meteor中是不可能的,因为它不支持findAndModify、upsert、聚合函数和map/reduce.

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?

谢谢

推荐答案

您需要手动将它们分组.有很多方法可以做到这一点,但这里有一个(诚然难以阅读)示例:

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});
});

这篇关于“分组依据"流星收集查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆