1

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

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 模型运行相同的命令,而无需定义connand db

4

1 回答 1

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

于 2012-07-25T19:58:14.100 回答