title: Mongo Aggregation : $group and $project array to object for counts
body: <p>I have documents like:</p>
  
  <pre><code>{
      "platform":"android",
      "install_date":20151029
  }
  </code></pre>
  
  <ol>
  <li>platform - can have one value from [android|ios|kindle|facebook ] . </li>
  <li>install_date - there are many install_dates</li>
  </ol>
  
  <p>There are also many fields.</p>
  
  <p><strong>Aim</strong> : I am calculating installs per platform on particular date.</p>
  
  <p>So I am using group by in aggregation framework and make counts by platform. Document should look like like:</p>
  
  <pre><code>{
    "install_date":20151029,
    "platform" : {
     "android":1000,
     "ios": 2000,
     "facebook":1500
    }
  }
  </code></pre>
  
  <hr>
  
  <p>I have done like:</p>
  
  <pre><code>db.collection.aggregate([
    { 
        $group: { 
            _id: { platform: "$platform",install_date:"$install_date"},  
              count: { "$sum": 1 } 
          }
    },
    { 
        $group: { 
            _id: { install_date:"$_id.install_date"},
            platform: { $push :  {platform :"$_id.platform", count:"$count" } }  
        }
    },
    { 
       $project : { _id: 0, install_date: "$_id.install_date", platform: 1 } 
    }     
  ]) 
  </code></pre>
  
  <p>which Gives document like:</p>
  
  <pre><code>{
      "platform": [
          {
              "platform": "facebook",
              "count": 1500
          },
          {
              "platform": "ios",
              "count": 2000
          },
          {
              "platform": "android",
              "count": 1000
          }
      ],
      "install_date": 20151027
  }
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>Projecting array to single object as "platform"</p>