title: MongoDB PHP Aggregating data + count + where
body: <p>I create a platform in PHP/MYsql and I am now migrating to mongo</p>
  
  <p>My old query for mysql :</p>
  
  <pre><code>select sum(game_won) as game_won,count(id) as total,position
  from games_player_stats 
  where position &lt; 6 and position &gt; 0 and user_id = :pa_id 
  group by position 
  order by total desc
  </code></pre>
  
  <p>The new json format looks like this:</p>
  
  <pre><code>{
   "region" : "EUW",
   "players" : [
           {
              "position" : 2,
              "summoner_id" : 123456,
              "game_won": 1
           },
           {
              "position" : 1,
              "summoner_id" : 123459,
              "game_won": 0
           },
           {
              "position" : 3,
              "summoner_id" : 123458,
              "game_won": 1
           },
           {
              "position" : 4,
              "summoner_id" : 123457,
              "game_won": 0
           }
               ]
  }
  </code></pre>
  
  <p>Having multiple documents like this, I need to find howmany times summoner_id 123456 has had position 2 or any of the other positions 1-6 and howmany times did he win in that position</p>
  
  <p>The Index needs to be queryable on region and summoner_id</p>
  
  <p>Outcome would look like</p>
  
  <pre><code> {
     "positions" : 
           [
             { "position" : 1,
               "total" : 123,
               "won" : 65
             },
             { "position" : 2,
               "total" : 37,
               "won" : 10
             }
           ]
     }
  </code></pre>
  
  <p>Would I need to use Map/Reduce for this?</p>