尝试使用$proj时不会发生任何情况

2024-09-28 23:18:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是mongodb的新手,仍然坐在同一条管道上。我不明白为什么我对$project的使用根本没有产生任何输出?你知道吗

def make_pipeline():
    # complete the aggregation pipeline
    pipeline = [
    {
        '$match': { 
            "user.statuses_count": {"$gt":99 },
            "user.time_zone": "Brasilia"
        }
    },
    {
        "$group": {
            "_id": "$user.id",
            "followers": { "$max": "$user.followers_count" }

        }
    },
    {
        "$sort": { "followers": -1 }
    },{"$project": { 
    "userId": "$user.id",
    "screen_name": "$user.screen_name",
    "retweet_count": "$retweet_count"}},
    {
        "$limit" : 1
    }
]

有什么想法吗?你知道吗


Tags: nameprojectidmake管道pipelinemongodbdef
1条回答
网友
1楼 · 发布于 2024-09-28 23:18:55

尝试下面的聚合管道,它将为您提供所需的输出。你知道吗

使用Mongo shell: 测试文档(具有最小测试用例):

db.tweet.insert([
    {
        "retweet_count" : 23,
        "user" : {
            "time_zone" : "Brasilia",
            "statuses_count" : 2475,
            "screen_name" : "Catherinemull",
            "followers_count" : 169,
            "id" : 37486277
        },   
        "id" : NumberLong("22819398300")
    },
        {
        "retweet_count" : 7,
        "user" : {
            "time_zone" : "Lisbon",
            "statuses_count" : 4532,
            "screen_name" : "foo",
            "followers_count" : 43,
            "id" : 37486278
        },   
        "id" : NumberLong("22819398301")
    },
        {
        "retweet_count" : 12,
        "user" : {
            "time_zone" : "Brasilia",
            "statuses_count" : 132,
            "screen_name" : "test2",
            "followers_count" : 4,
            "id" : 37486279
        },   
        "id" : NumberLong("22819398323")
    },
        {
        "retweet_count" : 4235,
        "user" : {
            "time_zone" : "Brasilia",
            "statuses_count" : 33,
            "screen_name" : "test4",
            "followers_count" : 2,
            "id" : 37486280
        },   
        "id" : NumberLong("22819398308")
    },
    {
        "retweet_count" : 562,
        "user" : {
            "time_zone" : "Kenya",
            "statuses_count" : 672,
            "screen_name" : "Kiptot",
            "followers_count" : 169,
            "id" : 37486281
        },   
        "id" : NumberLong("22819398374")
    },
    {
        "retweet_count" : 789,
        "user" : {
            "time_zone" : "Brasilia",
            "statuses_count" : 5263,
            "screen_name" : "test231",
            "followers_count" : 8282,
            "id" : 37486
        },   
        "id" : NumberLong("22819398331")
    }
]);

魔法:

db.tweet.aggregate([
    {
        '$match': { 
            "user.statuses_count": {"$gt":99 },
            "user.time_zone": "Brasilia"
        }
    },
    {
        "$group": {
            "_id": "$user.id",
            "followers": { "$max": "$user.followers_count" },
            "doc": {
                "$addToSet": "$$ROOT"
            }
        }
    },
    {
        "$sort": { "followers": -1 }
    },
    {
        "$unwind": "$doc"
    },
    {
        "$project": {
            "_id": 0,
            "userId": "$_id",
            "screen_name": "$doc.user.screen_name",
            "retweet_count": "$doc.retweet_count",
            "followers": 1
        }
    },
    {
        "$limit": 1
    }
]);

输出:

/* 1 */
{
    "result" : [ 
        {
            "userId" : 37486,
            "screen_name" : "test231",
            "retweet_count" : 789,
            "followers" : 8282
        }
    ],
    "ok" : 1
}

更新

Python实现

>>> from bson.son import SON
>>> pipeline = [
...     {"$match": {"user.statuses_count": {"$gt": 99}, "user.time_zone": "Brasilia"}},
...     {"$group": {"_id": "$user.id", "followers": { "$max": "$user.followers_count" }, "doc": {"$addToSet": "$$ROOT"}}},
...     {"$sort": {"followers": -1 }},
...     {"$unwind": "$doc"}, {"$project": {"_id": 0, "userId": "$_id", "screen_name": "$doc.user.screen_name", "retweet_count": "$doc.retweet_count", "followers": 1}},
...     {"$limit": 1}
... ]
>>> list(db.tweet.aggregate(pipeline))
[{u'userId': 37486, u'screen_name': u'test231', u'retweet_count': 789, u'followers': 8282}]

相关问题 更多 >