简体   繁体   English

Mongoid Group By或MongoDb group by in rails

[英]Mongoid Group By or MongoDb group by in rails

I have a mongo table that has statistical data like the following.... 我有一个mongo表,其中包含如下统计数据....

so my class is as follows... 所以我的班级如下......

class Statistic
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  field :course_id, type: Integer
  field :status, type: String # currently this is either play or complete

I want to get a daily count of total # of plays for a course. 我想每天获得一个课程总数的数量。 So for example... 8/1/12 had 2 plays, 8/2/12 had 6 plays. 所以例如... 8/1/12有2个剧本,8/2/12有6个剧本。 Etc. I would therefore be using the created_at timestamp field, with course_id and action. 等等。因此,我将使用created_at时间戳字段,使用course_id和action。 The issue is I don't see a group by method in Mongoid. 问题是我没有看到Mongoid中的group by方法。 I believe mongodb has one now, but I'm unsure of how that would be done in rails 3. 我相信mongodb现在有一个,但我不确定如何在rails 3中完成。

I could run through the table using each, and hack together some map or hash in rails with incrementation, but what if the course has 1 million views, retrieving and iterating over a million records could be messy. 我可以使用每个来运行表格,并在轨道中加入一些地图或散列,但是如果课程有100万个视图,那么检索和迭代超过一百万条记录可能会很麻烦。 Is there a clean way to do this? 有干净的方法吗?

As mentioned in comments you can use map/reduce for this purpose. 如评论中所述,您可以使用map / reduce来实现此目的。 So you could define the following method in your model ( http://mongoid.org/en/mongoid/docs/querying.html#map_reduce ) 因此,您可以在模型中定义以下方法( http://mongoid.org/en/mongoid/docs/querying.html#map_reduce

def self.today
  map = %Q{
    function() {
      emit(this.course_id, {count: 1})
    }
  }

  reduce = %Q{
    function(key, values) {
      var result = {count: 0};
      values.forEach(function(value) {
        result.count += value.count;
      });
      return result;
    }
  }

  self.where(:created_at.gt => Date.today, status: "played").
    map_reduce(map, reduce).out(inline: true)
end

which would result in following result: 这将导致以下结果:

[{"_id"=>1.0, "value"=>{"count"=>2.0}}, {"_id"=>2.0, "value"=>{"count"=>1.0}}] 

where _id is the course_id and count is the number of plays. 其中_idcourse_idcount是播放次数。

There is also dedicated group method in MongoDB but I am not sure how to get to the bare mongodb collection in Mongoid 3. I did not have a chance to dive into code that much yet. 在MongoDB中也有专门的方法,但我不知道如何到Mongoid 3中的裸mongodb集合。我还没有机会深入研究代码。

You may wonder why I emit a document {count: 1} as it does not matter that much and I could have just emitted empty document or anything and then always add 1 to the result.count for every value. 你可能想知道为什么我发出一个文件{count: 1}因为它没那么重要,我可能只是发出空文档或任何东西,然后总是为每个值的result.count添加1。 The thing is that reduce is not called if only one emit has been done for particular key (in my example course_id has been played only once) so it is better to emit documents in the same format as result. 问题是如果对特定键只进行了一次发射,则不调用reduce(在我的示例中, course_id只播放了一次),因此最好以与结果相同的格式发出文档。

Using Mongoid 使用Mongoid

stages =  [{ 
         "$group" => {  "_id" => { "date_column_name"=>"$created_at" }},
         "plays_count" => { "$sum" => 1 }
    }]
@array_of_objects = ModelName.collection.aggregate(stages, {:allow_disk_use => true})

OR 要么

stages =  [{ 
          "$group" => {  
             "_id" => { 
                       "year" => { "$year" => "$created_at" },
                       "month" => { "$month" => "$created_at" },
                       "day" => { "$dayOfMonth" => "$created_at" }
              }
           },
          "plays_count" => { "$sum" => 1 }
    }]
@array_of_objects = ModelName.collection.aggregate(stages, {:allow_disk_use => true})

Follow the links below to group by using mongoid 按照以下链接使用mongoid进行分组

https://taimoorchangaizpucitian.wordpress.com/2016/01/08/mongoid-group-by-query/ https://docs.mongodb.org/v3.0/reference/operator/aggregation/group/ https://taimoorchangaizpucitian.wordpress.com/2016/01/08/mongoid-group-by-query/ https://docs.mongodb.org/v3.0/reference/operator/aggregation/group/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM