由于视频有 embeds_many 个日期,而日期有 embeds_many 个国家/地区。使用聚合框架,我将如何获得所有的总数likes_count
?
我得到以下结构:
{ "_id" : ObjectId( "5190fbbc72357e713900001b" ),
"dates" : [
{ "_id" : ObjectId( "5190fbbc72357e713900001c" ),
"countries" : [
{ "_id" : ObjectId( "5190fbbc72357e713900001d" ),
"unique_views_count" : 500,
"non_unique_views_count" : 1000,
"likes_count" : 1,
"comments_count" : 1,
"iso_two_letter_country_code" : "US",
"country_name" : "United States" },
{ "_id" : ObjectId( "5190fbbc72357e713900001e" ),
"unique_views_count" : 300,
"non_unique_views_count" : 777,
"likes_count" : 0,
"comments_count" : 0,
"iso_two_letter_country_code" : "UK",
"country_name" : "United Kingdom" } ],
"date" : 20130513 } ],
"video_id" : 1 }
到目前为止,我已经尝试过,但无济于事:
Video.collection.aggregate(
{ '$unwind' => '$dates.countries'},
{
'$group' => {
'_id' => '$_id'
'likes' => { '$sum' => '$dates.countries.likes_count' }
}
}
)