代码之家  ›  专栏  ›  技术社区  ›  Wylliam Judd

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

  •  1
  • Wylliam Judd  · 技术社区  · 6 年前

    以下是我的工作代码:

    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版

    0 回复  |  直到 6 年前