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

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

问题描述

我的数据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/,但显然在流星中是不可能的,因为它不支持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天全站免登陆