How do I group by _id and country?
===

I need to group by _id and countries. I've managed to group by `_id`, but would like to know how to group the countries in these `_id`s and return the count for each country.

I am using the aggregation framework. So far so good.

    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']

The following returns:

    [
        [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"
                }
            ]
        }
    ]