title: aggregating metrics data in mongodb
body: <p>I'm trying to pull report data out of a realtime metrics system inspired by the <a href="https://speakerdeck.com/mongodb/schemas-for-real-time-analytics-with-nodejs-eric-lubow-and-russell-bradberry-simplereach" rel="nofollow">NYC MUG/SimpleReach</a> schema, and maybe my mind is still stuck in SQL mode.</p>
  
  <p>The data is stored in a document like so...</p>
  
  <pre><code>{
  "_id": ObjectId("5209683b915288435894cb8b"),
  "account_id": 922,
  "project_id": 22492,
  "stats": {
      "2009": {
          "04": {
              "17": {
                  "10": {
                      "sum": {
                          "impressions": 11
                      }
                  },
                  "11": {
                      "sum": {
                          "impressions": 603
                      }
                  },
               },
           },
       },
   }}
  </code></pre>
  
  <p>and I've been trying different variations of the aggregation pipeline with no success.</p>
  
  <pre><code>db.metrics.aggregate({
  $match: {
      'project_id':22492
  }}, {
  $group: {
      _id: "$project_id",
      'impressions': {
  
           //This works, but doesn't sum up the data...
           $sum: '$stats.2009.04.17.10.sum.impressions'
  
           /* none of these work.
           $sum: ['$stats.2009.04.17.10.sum.impressions',          
                '$stats.2009.04.17.11.sum.impressions']
  
           $sum: {'$stats.2009.04.17.10.sum.impressions',          
                '$stats.2009.04.17.11.sum.impressions'}
  
          $sum: '$stats.2009.04.17.10.sum.impressions',          
                '$stats.2009.04.17.11.sum.impressions'
          */
      }
  }
  </code></pre>
  
  <p>any help would be appreciated. </p>
  
  <p>(ps. does anyone have any ideas on how to do date range searches using this document schema? )</p>