mongodb - group by using mongoid in rails -


i using mongoid rails4, need group result supplying charts (statistics screen) using below code.

    @comments_stats = {}      comments_data = comment.where(:created_at.lte => date.today-10.days).group_by {|d| d.created_at.to_date }      comments_data.map{ |a| @comments_stats[a[0].strftime("%d-%m-%y")] = a[1].size} 

it give

{ "1-1-2014" => 2, "3-1-2014" => 1, "4-1-2014" => 2, "6-1-2014" => 4  } 

but want below

{ "1-1-2014" => 2, "2-1-2014" => 0, "3-1-2014" => 1, "4-1-2014" => 2, "5-1-2014" => 0, "6-1-2014" => 4  } 

any 1 suggest how simplify above query also.

thanks prasad.

you can try this:

@comments_stats = {} last_date = date.today - 10.days  comments_data = comment.where(:created_at.lte => last_date).group_by {|d| d.created_at.strftime("%d-%m-%y")} first_date = date.parse(comments_data.keys.min)  (first_date..last_date).map |n_date|   day = n_date.strftime("%d-%m-%y")   @comments_stats[day] = comments_data[day] ? comments_data[day].size : 0 end 

i haven't tested can have issue


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -