【问题标题】:How to "CAST" a Time field as Date in Mongoid如何在 Mongoid 中将时间字段“投射”为日期
【发布时间】:2013-06-24 08:03:41
【问题描述】:

我使用 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;

到目前为止,我能够通过聚合解决我的大部分组/总和问题,但在这种情况下,我不知道该怎么做。

【问题讨论】:

标签: ruby-on-rails mongodb mongoid


【解决方案1】:

MongoDB 现在提供了一个聚合框架,使这更容易一些。 Mongoid 还提供了一个到 MongoDB map/reduce 框架的接口,您必须使用它来执行这些聚合。见 this description of the aggregation framework 和 Mongoid 的 explanation of the map/reduce interface

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 2014-06-16
    • 2012-04-13
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多