集計フレームワークを使用しています。ここまでは順調ですね。
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つの結果があります。それは2つに分類され_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"
}
}
]
conn
次に、とを定義せずに、Railsモデルを使用して同じコマンドを実行するにはどうすればよいdb
ですか?