简体   繁体   English

将$ push与$ group和pymongo一起使用

[英]Using $push with $group with pymongo

Objective 目的

Fix my make_pipeline() function to, using an aggregation query, count the number of tweets for each user, add them to an array and return the 5 users with the most tweets. 修复我的make_pipeline()函数,以使用聚合查询来计算每个用户的tweet数量,将它们添加到数组中,并返回5个具有最高tweets的用户。

Exercise 行使

Using an aggregation query, count the number of tweets for each user. 使用聚合查询,计算每个用户的推文数量。 In the same $group stage, use $push to accumulate all the tweet texts for each user. 在同一$group阶段,使用$push累积每个用户的所有tweet文本。

Limit your output to the 5 users with the most tweets. 将您的输出限制为鸣叫最多的5个用户。

Your result documents should include only the fields: 您的结果文档应仅包括以下字段:

  • "_id" (screen name of user), "_id" (用户的屏幕名称),
  • "count" (number of tweets found for the user), "count" (为用户找到的推文数量),
  • "tweet_texts" (a list of the tweet texts found for the user). "tweet_texts" (为用户找到的tweet文本的列表)。

Background 背景

To achieve the previous objective I am testing the following code: 为了实现先前的目标,我正在测试以下代码:

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

Logic 逻辑

First I group all the tweets by username . 首先,我按username名将所有推文分组。 Then, in the same stage, I push all the texted tweets to tweet_texts and I count each occurrence that was grouped. 然后,在同一阶段,我将所有发短信的推文推送到tweet_texts并计算每个分组的事件。 I believe this will give me the number of users with most tweets. 我相信这会给我带来最多推文的用户数量。

Then I make a projection to select only the three fields I want: 然后,我进行投影以仅选择我想要的三个字段:

  • _id _ID
  • count 计数
  • tweet_texts tweet_texts

I finish by sorting and limiting the amount of results. 最后,我对结果进行排序和限制。

Problem 问题

I am passing the test, but not the submission. 我通过了考试,但未通过提交。 What am I doing wrong? 我究竟做错了什么? I now the error must be in the first (group) stage, but I can't find for the love of God what I am doing wrong. 我现在的错误一定是在第一个(小组)阶段,但是对于上帝的爱,我找不到我做错了什么。

Data Sample 数据样本

{
    "_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")
}

Please help! 请帮忙!

The $project step is redundant as the $group pipeline already produces just those three fields so there's no need for a preceding $project stage. $project步骤是多余的,因为$group管道已经只产生了这三个字段,因此不需要前面的$project阶段。

The correct pipeline should be 正确的管道应该是

pipeline = [ 
    {
        "$group": {
            "_id": "$user.screen_name", 
            "tweet_texts": { "$push": "$text" }, 
            "count": { "$sum": 1 }
        }
    }, 
    { "$sort" : { "count" : -1 } }, 
    { "$limit": 5 } 
] 

Your $project pipeline didn't work because the previous $group pipeline doesn't produce any field "$user.screen_name" which you attempt to use as the _id field in the $project pipeline. 您的$project管道不起作用,因为先前的$group管道不会产生任何字段"$user.screen_name" ,您尝试将其用作$project管道中的_id字段。

However, if you wanted to include the $project step then the working pipeline should follow: 但是,如果要包括$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 } 
] 

Reading comments 阅读评论

Reading the comments I found out that 阅读评论,我发现

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}
    ]

Should in fact be changed to: 实际上应该更改为:

pipeline = [ 
        {"$group": {"_id": "$user.screen_name", "tweet_texts": {"$push": "$text"}, "count": {"$sum": 1}}}, 
        {"$sort" : {"count" : -1}}, 
        {"$limit": 5}
    ]

Why? 为什么?

The full answer and explanation can be seen in the answer: 完整的答案和解释可以在答案中看到:

The conclusion of the story is that I am using the $project stage wrongly. 故事的结论是我错误地使用了$project阶段。 Not only was is no needed in the first place, to make it idempotent it should be 首先,不仅不需要,它应该是幂等的。

{"$project": {"_id": "$_id", "count": 1, "tweet_texts": 1}},

I also highly recommend his answer: 我也强烈建议他回答:

Special Thanks 特别感谢

The following users deserve kudos++: 以下用户应获得荣誉++:

For directing me in to the right path! 引导我进入正确的道路!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM