我如何按_id分组?

时间:2012-07-25 19:37:13

标签: ruby-on-rails ruby ruby-on-rails-3 mongodb

我正在使用聚合框架。到目前为止一切都很好。

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

cmd = {
  aggregate: 'live_daily_stats',
  pipeline: [
    # { '$match' => { :_id => "20120725/foobar/song/custom-cred" } },
    { '$project' => {
      :visits => 1,
    } },
    { '$unwind' => '$visits' },
    # { '$group' => {
    #   :_id => '$_id'
    # } },
    { '$match' => { 'visits.minute' => { '$gt' => 224 } } },
    { '$sort' => { 'visits.minute' => 1 } },
    # { '$group' => { :_id => '$_id' } },
  ]
}

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

results返回以下内容。如您所见,有4个结果。那个属于两个_ids。如何将visits归为_ids

[
    [0] {
           "_id" => "20120725/foobar/song/custom-cred",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                               "referer" => "http://localhost:3000/cfazzini/songs/custom-cred",
                                                "minute" => 251,
                                              "token_id" => "134318948211lzyqexgo"
        }
    },
    [1] {
           "_id" => "20120725/foobar/song/custom-cred",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                               "referer" => "http://localhost:3000/cfazzini/songs/custom-cred",
                                                "minute" => 1118,
                                              "token_id" => "134324148411hsrxvakn"
        }
    },
    [2] {
           "_id" => "20120725/foobar/song/test-pg3-long-title-here-test-lorem-ipsum-dolor-lo",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                               "referer" => "http://localhost:3000/",
                                                "minute" => 1121,
                                              "token_id" => "13432416893tlfsmmgh"
        }
    },
    [3] {
           "_id" => "20120725/foobar/song/custom-cred",
        "visits" => {
                                          "country_name" => "UK",
                           "iso_two_letter_country_code" => "UK",
                                               "referer" => "http://localhost:3000/",
                                                "minute" => 1121,
                                              "token_id" => "134324169011hmtkxrgt"
        }
    }
]

其次,如何使用rails模型运行相同的命令,而不必定义conndb

1 个答案:

答案 0 :(得分:2)

1)您需要使用$push运算符 - 例如:

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

2)您可以像这样获取连接:Mongoid.database.connection而不是必须定义数据库和连接。总计助手即将到来 - 目前正在进行中。请关注此票:https://jira.mongodb.org/browse/RUBY-455