我正在尝试返回大于 12visits
的值。minute
我的数据结构如下所示:
{ "_id" : "20120723/foobar/song/custom-cred",
"live_daily_count" : 4,
"metacontent" : { "date" : "20120723",
"live_daily_statable_slug" : "custom-cred",
"live_daily_statable_title" : "custom cred",
"live_daily_statable_type" : "Song",
"url" : "foobar/songs/custom-cred",
"user_slug" : "foobar" },
"visits" : [
{ "country_name" : "UK",
"iso_two_letter_country_code" : "UK",
"referer" : "http://localhost:3000/foobar/songs/no-title-with-space",
"minute" : 12,
"token_id" : "134300236111rcbmbmvv" },
{ "country_name" : "UK",
"iso_two_letter_country_code" : "UK",
"referer" : "http://localhost:3000/foobar/songs/no-title-with-space",
"minute" : 13,
"token_id" : "134300242111pjvkjjkf" },
{ "country_name" : "UK",
"iso_two_letter_country_code" : "UK",
"referer" : "http://localhost:3000/foobar/songs/no-title-with-space",
"minute" : 13,
"token_id" : "134300243511udbnqldm" }
]
}
我正在使用 Mongodb Ruby 驱动程序。我尝试了以下方法:
conn = Mongo::Connection.new
db = conn['foobar_development']
cmd = {
aggregate: 'live_daily_stats',
pipeline: [
{ '$match' => { :_id => "20120723/foobar/song/custom-cred" } },
{ '$project' => {
:visits => 1,
} },
{ '$unwind' => '$visits' },
# { '$group' => {
# :_id => '$_id'
# } },
]
}
res = db.command(cmd)['result']
它现在返回:
[
[0] {
"_id" => "20120723/foobar/song/custom-cred",
"visits" => {
"country_name" => "UK",
"iso_two_letter_country_code" => "UK",
"referer" => "http://localhost:3000/foobar/songs/custom-cred",
"minute" => 12,
"token_id" => "134300236111rcbmbmvv"
}
},
[1] {
"_id" => "20120723/foobar/song/custom-cred",
"visits" => {
"country_name" => "UK",
"iso_two_letter_country_code" => "UK",
"follower_class" => "non_follower",
"referer" => "http://localhost:3000/foobar/songs/custom-cred",
"minute" => 13,
"token_id" => "134300242111pjvkjjkf"
}
},
[2] {
"_id" => "20120723/foobar/song/custom-cred",
"visits" => {
"country_name" => "UK",
"iso_two_letter_country_code" => "UK",
"follower_class" => "non_follower",
"referer" => "http://localhost:3000/foobar/songs/custom-cred",
"minute" => 13,
"token_id" => "134300243511udbnqldm"
}
}
]
如何确保结果只返回visits
大于minute
12 的?任何帮助,将不胜感激。