0

我需要按 _id 和国家/地区分组。我已经设法分组_id,但想知道如何对这些国家/地区进行分组_id并返回每个国家/地区的计数。

我正在使用聚合框架。到现在为止还挺好。

conn = Mongo::Connection.new
db   = conn['foobar_development']

cmd = {
  aggregate: 'live_daily_stats',
  pipeline: [
    { '$project' => {
      :metacontent => 1,
      :visits => 1,
    } },
    { '$unwind' => '$visits' },
    { '$match' => { 'visits.minute' => { '$gt' => 224 } } },
    { '$sort' => { 'visits.minute' => 1 } },
    { '$group' => { 
      :_id => '$_id', 
      :visits => { '$push' => '$visits' }, 
      :visits_count => { '$sum' => 1 },
      :metacontent => { '$addToSet' => '$metacontent' },
      } 
    },
    { '$sort' => { 'visits_count' => -1 } },
  ]
}

res = db.command(cmd)['result']

以下返回:

[
    [0] {
                 "_id" => "20120726/foobar/song/custom-cred",
              "visits" => [
            [0] {
                                              "country_name" => "UK",
                               "iso_two_letter_country_code" => "UK",
                                                   "referer" => "http://localhost:3000/",
                                                    "minute" => 59,
                                                  "token_id" => "134326199711wfryhpdq"
            },
            [1] {
                                              "country_name" => "UK",
                               "iso_two_letter_country_code" => "UK",
                                                   "referer" => "http://localhost:3000/",
                                                    "minute" => 59,
                                                  "token_id" => "134326199711wfryhpdq"
            },
            [2] {
                                              "country_name" => "US",
                               "iso_two_letter_country_code" => "US",
                                                   "referer" => "http://localhost:3000/",
                                                    "minute" => 59,
                                                  "token_id" => "134326199711wfryhpdq"
            }
        ],
        "visits_count" => 1,
         "metacontent" => [
            [0] {
                                     "date" => "20120726"
            }
        ]
    },
    [1] {
                 "_id" => "20120725/foobar/song/test-pg3-long-title-here-test-lorem-ipsum-dolor-lo",
              "visits" => [
            [0] {
                                              "country_name" => "UK",
                               "iso_two_letter_country_code" => "UK",
                                                   "referer" => "http://localhost:3000/",
                                                    "minute" => 58,
                                                  "token_id" => "13432600883knjzcbic"
            }
        ],
        "visits_count" => 1,
         "metacontent" => [
            [0] {
                                     "date" => "20120725"
            }
        ]
    }
]
4

2 回答 2

0

文档

$group 将文档分组在一起,以便根据文档集合计算聚合值。实际上,组通常支持诸如每天网站中每个页面的平均页面浏览量之类的任务。

$group 的输出取决于您如何定义组。首先为您使用此管道创建的组指定一个标识符(即 _id 字段)。您可以指定管道中文档中的单个字段、先前计算的值或由多个传入字段组成的聚合键。

每个组表达式都必须指定一个 _id 字段。您可以将 _id 字段指定为带点的字段路径引用、包含在大括号中的多个字段(即 { 和 })的文档或常量值。

我会先尝试按 _id 和国家/地区分组(让您进行所需的计数),然后仅按 _id 对结果进行分组以提供所需的结构。

更新:

我在想这样的事情..但我没有环境设置来检查它..

    conn = Mongo::Connection.new
    db   = conn['foobar_development']

    cmd = {
      aggregate: 'live_daily_stats',
      pipeline: [
        { '$project' => {
          :metacontent => 1,
          :visits => 1,
        } },
        { '$unwind' => '$visits' },
        { '$match' => { 'visits.minute' => { '$gt' => 224 } } },
        { '$sort' => { 'visits.minute' => 1 } },
        { '$group' => { 
          :_id => {'$_id','$visits.iso_two_letter_country_code'},
          :page_id => '$_id',
          :visits_count => { '$sum' => 1 },
   .... whatever you want ...
          :metacontent => { '$addToSet' => '$metacontent' },
          } 
        },
        { '$group' => { 
          :_id => '$page_id', 
   .... whatever you want ...
          } 
        },
        { '$sort' => { 'visits_count' => -1 } },
      ]
    }

    res = db.command(cmd)['result']
于 2012-07-26T01:34:21.500 回答
0

我将 更改$group为连接两者_idcountry_name

cmd = {
  aggregate: 'live_daily_stats',
  pipeline: [
    { '$project' => {
      :metacontent => 1,
      :visits => 1,
    } },
    { '$unwind' => '$visits' },
    { '$match' => { 'visits.minute' => { '$gt' => 224 } } },
    { '$sort' => { 'visits.minute' => 1 } },
    { '$group' => { 
      :_id => { '$add' => ['$_id', '$visits.country_name']}, 
      :visits => { '$push' => '$visits' }, 
      :visits_count => { '$sum' => 1 },
      :metacontent => { '$addToSet' => '$metacontent' },
      } 
    },
    { '$sort' => { 'visits_count' => -1 } },
  ]
}
于 2012-07-26T10:15:26.407 回答