我正在使用聚合框架。到目前为止,一切都很好。
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 模型运行相同的命令,而无需定义conn
and db
?