以下是我的工作代码:
age_range = DemographicsData.collection.aggregate([
{ '$match' => { video_id: { '$in' => video_ids }}},
{ '$unwind' => '$demographics_labels' },
{ '$group' => { _id: '$demographics_labels.age_range', count: { '$sum' => 1 }}}
])
gender = DemographicsData.collection.aggregate([
{ '$match' => { video_id: { '$in' => video_ids }}},
{ '$unwind' => '$demographics_labels' },
{ '$group' => { _id: '$demographics_labels.gender', count: { '$sum' => 1 }}}
])
ethnicity = DemographicsData.collection.aggregate([
{ '$match' => { video_id: { '$in' => video_ids }}},
{ '$unwind' => '$demographics_labels' },
{ '$group' => { _id: '$demographics_labels.ethnicity', count: { '$sum' => 1 }}}
])
{
age: age_range,
gender: gender,
ethnicity: ethnicity,
}
Rails 3.2.8版
Ruby 1.9.3版