如何用不同的密钥对给定密钥和组的值进行求和? Pymongo / MongoDB的

问题描述 投票:0回答:1

如果问题令人困惑,我很抱歉,我不完全确定如何说出问题。我希望能从这个例子中明白我想要做的事情。

我有一个应用程序包含一些有关音乐曲目的数据。用户上传曲目,人们对其进行投票。这是我的曲目集合的一部分:

{
    "_id": {
        "$oid": "5c98e400c0b156250eeda15c"
    },
    "track_title": "I Heard It Through The Grapevine",
    "description": "A nice track description",
    "date_added": "25 March 2019 14:52",
    "artist": "Marvin Gaye",
    "genre": "Soul",
    "upvotes": 10,
    "youtube_link": "https://www.youtube.com/watch?v=hajBdDM2qdg",
    "year": 1968,
    "user_name": "Christine"
}

{
    "_id": {
        "$oid": "5c990754c0b1563bbbc577a2"
    },
    "track_title": "Snow",
    "description": "Foo bar spam eggs",
    "date_added": "25 March 2019 16:36",
    "artist": "Red Hot Chili Peppers",
    "genre": "Alternative Rock",
    "upvotes": 6,
    "youtube_link": "https://www.youtube.com/watch?v=ifXalt3MJtM",
    "year": 2006,
    "date_added_raw": {
        "$date": "2019-03-25T16:36:52.988Z"
    },
    "user_name": "Frazer"
}

{
    "_id": {
        "$oid": "5c938e11c0b15662ec3c79de"
    },
    "track_title": "Californication",
    "description": "I am foo, bar are you?",
    "date_added": "21 March 2019 13:53",
    "artist": "Red Hot Chili Peppers",
    "genre": "Alt Rock",
    "upvotes": 10,
    "youtube_link": "https://www.youtube.com/watch?v=YlUKcNNmywk",
    "year": 2000,
    "user_name": "Ewan M"
}

我要做的是找到整个系列中最受欢迎的艺术家。

理想情况下,该过程将类似于:

  1. 获得每位艺术家的总投票数
  2. 按升值排序

在上面的示例集合的情况下,我会得到类似的东西:

{
    "artist": "Red Hot Chili Peppers",
    "total_upvotes": 16,
}

{
    "artist": "Marvin Gaye",
    "total_upvotes": 10,
}

然后,我可以使用最终的收藏品,不仅获得最受欢迎的艺术家,也可能获得第二和第三最受欢迎的艺术家。

到目前为止,我能够总结整个系列中的所有赞成票(即所有曲目的总票数)

# Get the total number of upvotes cast on the app
# First, get a list of all the upvote values
all_upvotes_list = list(tracks_collection.find( { },
                                { 'upvotes': 1, '_id' :0 }
                            ))

# Iterate through the list and sum the values
all_upvotes = sum(item['upvotes'] for item in all_upvotes_list)

但我不完全确定从哪里拿走它,或者即使我朝着正确的方向前进。有任何想法吗?

谢谢!

python mongodb mongodb-query pymongo
1个回答
0
投票

解决了它,最终很容易。

按艺术家分组,然后总结一下:

db.tracks.aggregate([
                     { '$group': { '_id': "$artist", 'total': { '$sum': '$upvotes' } } },
                     { '$sort': { 'total': -1 } }
                   ])

推荐问答