title: Trouble using $group twice in mongodb aggregation query
body: <p>Let me start off by saying I am new to mongo and node.js. For each client I work with, I am looking at the past week of traffic to find the max and min of six variables and their dates. Right now mongo aggregation takes care of some of the work but I am convinced that it can do more. Right now my pipeline is the following:</p>
  
  <ol>
  <li>$match - only return documents within the past week</li>
  <li>$group - group by client and by date</li>
  <li>$project - calculate some values</li>
  </ol>
  
  <p>And documents are returned in the following manor:</p>
  
  <pre><code>{ _id: 
     { client: "aRandomClientNameWouldBeHere",
       date: Thu Jun 04 46370 00:00:00 GMT-0400 (EDT) },
    impSales: 0,
    pixSales: 0,
    apiSales: 0,
    s2sSales: 0,
    clicks: 3,
    impressions: 0 }
  </code></pre>
  
  <p>I am having trouble adding another $group stage to the pipeline, grouping over just clients and getting the dates that the max and min occur for each field.</p>
  
  <p>I need something like this:</p>
  
  <pre><code>$group: { 
    _id: "$client" ,
    maxImpSales: { $max: impSales },
    maxImpSalesDate : { ??? },
    minImpSales: { $min: impSales },
    minImpSalesDate : { ??? },
    ...
  }
  </code></pre>
  
  <p>However, I cant get it to work for two reasons. </p>
  
  <ol>
  <li>I don't know how to group just by client since client is now in the _id field along with date. (i.e. do I use _id: "$client" or _id : _id.client or something else?) </li>
  <li>How can I embed the date that the min and the max occurred?</li>
  </ol>
  
  <p>How do I do 1 &amp; 2? Thanks. Below is my 3 stage pipeline</p>
  
  <pre><code>{
    $match:
    {
      date:
      {
        $gte: _.last(dutil.lastXDates(6))
      }
    }
  },
  {
    $group:
    {
      _id:
      {
        client: "$client",
        date: "$date"
      },
      z1is: { $sum: "$actions.sales.import.z1.count" },
      z2is: { $sum: "$actions.sales.import.z2.count" },
      z3is: { $sum: "$actions.sales.import.z3.count" },
      z1ps: { $sum: "$actions.sales.pixel.z1.count" },
      z2ps: { $sum: "$actions.sales.pixel.z2.count" },
      z3ps: { $sum: "$actions.sales.pixel.z3.count" },
      z1as: { $sum: "$actions.sales.apiOnly.z1.count" },
      z2as: { $sum: "$actions.sales.apiOnly.z2.count" },
      z3as: { $sum: "$actions.sales.apiOnly.z3.count" },
      z1ss: { $sum: "$actions.sales.s2s.z1.count" },
      z2ss: { $sum: "$actions.sales.s2s.z2.count" },
      z3ss: { $sum: "$actions.sales.s2s.z3.count" },
      z1ic: { $sum: "$actions.click.import.z1.count" },
      z2ic: { $sum: "$actions.click.import.z2.count" },
      z3ic: { $sum: "$actions.click.import.z3.count" },
      z1pc: { $sum: "$actions.click.pixel.z1.count" },
      z2pc: { $sum: "$actions.click.pixel.z2.count" },
      z3pc: { $sum: "$actions.click.pixel.z3.count" },
      z1ac: { $sum: "$actions.click.apiOnly.z1.count" },
      z2ac: { $sum: "$actions.click.apiOnly.z2.count" },
      z3ac: { $sum: "$actions.click.apiOnly.z3.count" },
      z1sc: { $sum: "$actions.click.s2s.z1.count" },
      z2sc: { $sum: "$actions.click.s2s.z2.count" },
      z3sc: { $sum: "$actions.click.s2s.z3.count" },
      z1ii: { $sum: "$actions.display.import.z1.count" },
      z2ii: { $sum: "$actions.display.import.z2.count" },
      z3ii: { $sum: "$actions.display.import.z3.count" },
      z1pi: { $sum: "$actions.display.pixel.z1.count" },
      z2pi: { $sum: "$actions.display.pixel.z2.count" },
      z3pi: { $sum: "$actions.display.pixel.z3.count" },
      z1ai: { $sum: "$actions.display.apiOnly.z1.count" },
      z2ai: { $sum: "$actions.display.apiOnly.z2.count" },
      z3ai: { $sum: "$actions.display.apiOnly.z3.count" },
      z1si: { $sum: "$actions.display.s2s.z1.count" },
      z2si: { $sum: "$actions.display.s2s.z2.count" },
      z3si: { $sum: "$actions.display.s2s.z3.count" }
    }
  },
  {
    $project:
    {
      totalCountImpSales: { $add: ["$z1is", "$z2is", "$z3is"] },
      totalCountImpClicks: { $add: ["$z1ic", "$z2ic", "$z3ic"] },
      totalCountImpImpressions: { $add: ["$z1ii", "$z2ii", "$z3ii"] },
      totalCountPixSales: { $add: ["$z1ps", "$z2ps", "$z3ps"] },
      totalCountPixClicks: { $add: ["$z1pc", "$z2pc", "$z3pc"] },
      totalCountPixImpressions: { $add: ["$z1pi", "$z2pi", "$z3pi"] },
      totalCountApiSales: { $add: ["$z1as", "$z2as", "$z3as"] },
      totalCountApiClicks: { $add: ["$z1ac", "$z2ac", "$z3ac"] },
      totalCountApiImpressions: { $add: ["$z1ai", "$z2ai", "$z3ai"] },
      totalCountS2sSales: { $add: ["$z1ss", "$z2ss", "$z3ss"] },
      totalCountS2sClicks: { $add: ["$z1sc", "$z2sc", "$z3sc"] },
      totalCountS2sImpressions: { $add: ["$z1si", "$z2si", "$z3si"] }
    }
  }
  </code></pre>