一个查询中的多个连续组Mongodb

时间:2018-12-18 00:04:06

标签: ruby-on-rails ruby mongodb aggregation-framework mongoid

这是我的工作代码:

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,
}

是否有任何方法可以在对数据库的单个查询中获得相同的最终结果?

Mongo v2.4.9

Rails v3.2.8

Ruby v1.9.3

MongoId v3.1.0

期待在某个时候进行升级,但是解决方案必须考虑这些版本。

0 个答案:

没有答案