title: Iterating through array produced by MongoDB aggregation query
body: <p>Good afternoon all,</p>
  
  <p>I am having a really tough time working with aggregation queries in MongoDB 3.4. I have a problem that is asking me to do push the results of my aggregation query into an empty array called <code>categories</code> which I have been able to do successfully using this code: </p>
  
  <pre><code>var categories = [];
  
      database.collection("item").aggregate([{
          $group : {
              _id : "$category",
               num : {$sum : 1}
           }},
          {$sort:{_id:1}}]).toArray(function(err, data){
  
              categories.push(...data);
              callback(categories);
              console.log(categories);
          })
  
      }
  </code></pre>
  
  <p><code>categories</code> looks like this:</p>
  
  <pre><code>[ { _id: 'Apparel', num: 6 },
  { _id: 'Books', num: 3 },
  { _id: 'Electronics', num: 3 },
  { _id: 'Kitchen', num: 3 },
  { _id: 'Office', num: 2 },
  { _id: 'Stickers', num: 2 },
  { _id: 'Swag', num: 2 },
  { _id: 'Umbrellas', num: 2 } ]
  </code></pre>
  
  <p>Next I have the following task:</p>
  
  <blockquote>
  <pre><code>     In addition to the categories created by your aggregation query,
       include a document for category "All" in the array of categories
       passed to the callback. The "All" category should contain the total
       number of items across all categories as its value for "num". The
       most efficient way to calculate this value is to iterate through
       the array of categories produced by your aggregation query, summing        
       counts of items in each category.
  </code></pre>
  </blockquote>
  
  <p>The problem is that it seems like inside my <code>.toArray()</code> method the <code>data</code> parameter sometimes acts like an array and sometimes not. For example if I wanted to add perhaps just the value of the <code>num</code> key to the <code>categories</code> array like so: <code>categories.push(...data["num"])</code> I get an error stating <code>undefined is not iterable</code>. </p>
  
  <p>Since I cannot iterate over each <code>data.num</code> key I cannot extract it's value and add it to a running total of all <code>data.num</code> values.</p>
  
  <p>What am I not understanding about what is going on here?</p>