Mongoid Group By or MongoDb group by in rails

前端 未结 2 575
日久生厌
日久生厌 2020-12-10 08:41

I have a mongo table that has statistical data like the following....

  • course_id
  • status which is a string, played or completed
  • a
2条回答
  •  囚心锁ツ
    2020-12-10 09:23

    Using 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

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

提交回复
热议问题