1
我試圖用minute
大於12如何使用MongoDB中的聚合框架執行此查詢?
我的數據結構返回visits
看起來是這樣的:
{ "_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
比12 minute
更大的回報?任何幫助,將不勝感激。
難道你不想讓$放開字段$訪問(什麼是$ data?)。我認爲$ unwind只適用於數組(你的訪問不是)。 – Thilo 2012-07-25 01:15:24
查看我的編輯,在'$ unwind'上。仍然是'[]'的結果。其次,我應該改變我的數據結構,以便它使用數組而不是散列?有什麼辦法可以使用哈希來代替數組?爲什麼我使用哈希值沒有特別的理由。只是個人喜好 – 2012-07-25 01:25:39
你有沒有考慮過使用單獨的收集訪問日誌?這會使這個特定的查詢變得微不足道(當然,我們不知道你的其他查詢)。 – Thilo 2012-07-25 01:27:36