title: How to sum values of a given key and group by a different key? Pymongo/Mongodb
body: <p>Apologies if the question is confusing, I'm not entirely sure how to word the question. What I'm trying to do should be apparent from the example hopefully.</p>
  
  <p>I have an app consisting of some data regarding music tracks. Users upload a track and people vote on it. Here is a part of my tracks collection:</p>
  
  <pre><code>{
      "_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"
  }
  </code></pre>
  
  <p>What I'm trying to do is <strong>find the artist with the most upvotes</strong> across the collection.</p>
  
  <p>Ideally, the process would be something like:</p>
  
  <ol>
  <li>Get the total upvotes for each artist</li>
  <li>Sort by upvotes descending</li>
  </ol>
  
  <p>In the case of the example collection above, I would get something like:</p>
  
  <pre><code>{
      "artist": "Red Hot Chili Peppers",
      "total_upvotes": 16,
  }
  
  {
      "artist": "Marvin Gaye",
      "total_upvotes": 10,
  }
  </code></pre>
  
  <p>I could then work with the resulting collection, getting not only the most popular artist but maybe the second and third most popular as well.</p>
  
  <p>So far I'm able to sum all the upvotes across the collection (i.e. the total votes cast across all tracks)</p>
  
  <pre><code># 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)
  </code></pre>
  
  <p>But I'm not entirely sure where to take it from there, or even if I'm going in the right direction. Any ideas?</p>
  
  <p>Thanks!</p>