我使用Mongoid作为我的Rails应用程序的ORM,并且遇到一个分组问题。 集合/模型基本上是一个带时间戳的事务日志,我想根据时间戳的日期部分进行一些分组。
该模型如下所示:
class Bounty
include Mongoid::Document
field :ts, type: Time
field :char_id, type: Integer
field :bounty, type: Integer
index({ ts: 1, char_id: 1 }, { unique: true })
end
...而我正在寻找的分组将相当于:
SELECT CAST(ts as date), char_id, sum(bounty) from bounties group by CAST(ts as date), char_id;
到目前为止,我能够通过聚合解决我的大多数群组/总和问题,但在这种情况下我不知道该怎么做。
答案 0 :(得分:0)
MongoDB现在提供了一个聚合框架,使这更容易一些。 Mongoid还提供了MongoDB map / reduce框架的接口,您必须使用它来进行这些聚合。请参阅this description of the aggregation framework和Mongoid的explanation of the map/reduce interface