[英]Using $push with $group with pymongo
修復我的make_pipeline()
函數,以使用聚合查詢來計算每個用戶的tweet數量,將它們添加到數組中,並返回5個具有最高tweets的用戶。
使用聚合查詢,計算每個用戶的推文數量。 在同一$group
階段,使用$push
累積每個用戶的所有tweet文本。
將您的輸出限制為鳴叫最多的5個用戶。
您的結果文檔應僅包括以下字段:
"_id"
(用戶的屏幕名稱), "count"
(為用戶找到的推文數量), "tweet_texts"
(為用戶找到的tweet文本的列表)。 為了實現先前的目標,我正在測試以下代碼:
def make_pipeline():
# complete the aggregation pipeline
pipeline = [
{"$group": {"_id": "$user.screen_name", "tweet_texts": {"$push": "$text"}, "count": {"$sum": 1}}},
{"$project": {"_id": "$user.screen_name", "count": 1, "tweet_texts": 1}},
{"$sort" : {"count" : -1}},
{"$limit": 5}
]
return pipeline
首先,我按username
名將所有推文分組。 然后,在同一階段,我將所有發短信的推文推送到tweet_texts
並計算每個分組的事件。 我相信這會給我帶來最多推文的用戶數量。
然后,我進行投影以僅選擇我想要的三個字段:
最后,我對結果進行排序和限制。
我通過了考試,但未通過提交。 我究竟做錯了什么? 我現在的錯誤一定是在第一個(小組)階段,但是對於上帝的愛,我找不到我做錯了什么。
{
"_id" : ObjectId("5304e2e3cc9e684aa98bef97"),
"text" : "First week of school is over :P",
"in_reply_to_status_id" : null,
"retweet_count" : null,
"contributors" : null,
"created_at" : "Thu Sep 02 18:11:25 +0000 2010",
"geo" : null,
"source" : "web",
"coordinates" : null,
"in_reply_to_screen_name" : null,
"truncated" : false,
"entities" : {
"user_mentions" : [ ],
"urls" : [ ],
"hashtags" : [ ]
},
"retweeted" : false,
"place" : null,
"user" : {
"friends_count" : 145,
"profile_sidebar_fill_color" : "E5507E",
"location" : "Ireland :)",
"verified" : false,
"follow_request_sent" : null,
"favourites_count" : 1,
"profile_sidebar_border_color" : "CC3366",
"profile_image_url" : "http://a1.twimg.com/profile_images/1107778717/phpkHoxzmAM_normal.jpg",
"geo_enabled" : false,
"created_at" : "Sun May 03 19:51:04 +0000 2009",
"description" : "",
"time_zone" : null,
"url" : null,
"screen_name" : "Catherinemull",
"notifications" : null,
"profile_background_color" : "FF6699",
"listed_count" : 77,
"lang" : "en",
"profile_background_image_url" : "http://a3.twimg.com/profile_background_images/138228501/149174881-8cd806890274b828ed56598091c84e71_4c6fd4d8-full.jpg",
"statuses_count" : 2475,
"following" : null,
"profile_text_color" : "362720",
"protected" : false,
"show_all_inline_media" : false,
"profile_background_tile" : true,
"name" : "Catherine Mullane",
"contributors_enabled" : false,
"profile_link_color" : "B40B43",
"followers_count" : 169,
"id" : 37486277,
"profile_use_background_image" : true,
"utc_offset" : null
},
"favorited" : false,
"in_reply_to_user_id" : null,
"id" : NumberLong("22819398300")
}
請幫忙!
$project
步驟是多余的,因為$group
管道已經只產生了這三個字段,因此不需要前面的$project
階段。
正確的管道應該是
pipeline = [
{
"$group": {
"_id": "$user.screen_name",
"tweet_texts": { "$push": "$text" },
"count": { "$sum": 1 }
}
},
{ "$sort" : { "count" : -1 } },
{ "$limit": 5 }
]
您的$project
管道不起作用,因為先前的$group
管道不會產生任何字段"$user.screen_name"
,您嘗試將其用作$project
管道中的_id
字段。
但是,如果要包括$project
步驟,則工作流程應遵循:
pipeline = [
{
"$group": {
"_id": "$user.screen_name",
"tweet_texts": { "$push": "$text" },
"count": { "$sum": 1 }
}
},
{ "$project": { "count": 1, "tweet_texts": 1 } },
{ "$sort" : { "count" : -1 } },
{ "$limit": 5 }
]
閱讀評論,我發現
pipeline = [
{"$group": {"_id": "$user.screen_name", "tweet_texts": {"$push": "$text"}, "count": {"$sum": 1}}},
{"$project": {"_id": "$user.screen_name", "count": 1, "tweet_texts": 1}},
{"$sort" : {"count" : -1}},
{"$limit": 5}
]
實際上應該更改為:
pipeline = [
{"$group": {"_id": "$user.screen_name", "tweet_texts": {"$push": "$text"}, "count": {"$sum": 1}}},
{"$sort" : {"count" : -1}},
{"$limit": 5}
]
完整的答案和解釋可以在答案中看到:
故事的結論是我錯誤地使用了$project
階段。 首先,不僅不需要,它應該是冪等的。
{"$project": {"_id": "$_id", "count": 1, "tweet_texts": 1}},
我也強烈建議他回答:
以下用戶應獲得榮譽++:
引導我進入正確的道路!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.