title: Simple grouping example with aggregate framework in Mongodb
body: <p>So i apologize if this question exists, I can't find an answer that suits my needs, but I think it is quite simple, unfortunately I am still new to Mongo's non-basic features despite having dabbled with it off and on for the past 2 years.</p>
  
  <p><strong>My Data:</strong>
  I have a collection which is a pretty straightforward set of documents, each representing a line item of inventory for microchips, the chips all have part numbers, stock codes, datecodes and quantites</p>
  
  <p>(datecodes indicating when the item was produced: 0611 would usually mean June 2011 for those unfamiliar.)</p>
  
  <p>The part number is the most important piece of data to an ed user running a search, however, a part number may show up multiple times in the set because it is the stock code (which represents PN + date code) which represents an individual line item. Unfortunately the external end user doesn't give a flying fudge about the stock code.</p>
  
  <p>For this particular search I need to group all of the part numbers and return the total quantity for said part number - for this particular lookup I do not need the date codes.</p>
  
  <p>So essentially, from  these three docs:</p>
  
  <p>{pn:1234, qty:200, datecode:0611, stockcode:1}
  {pn:1234, qty:300, datecode:0411, stockcode:2}
  {pn:1254, qty:100, datecode:0601, stockcode:3}</p>
  
  <p>I need my query to yield:</p>
  
  <p>{pn:1234, qty:500}
  {pn:1254, qty:100}</p>
  
  <p><strong>My Problem:</strong>
  My problem is pretty simple, I can't seem to find an example that does such a simple group using the aggregate framework. Everything has more specific and complex (if only slightly) examples.</p>
  
  <p><strong>My Code:</strong>
  The code I am trying is this:</p>
  
  <pre><code>db.collection.aggregate( { $group :
                           { _id : "$pn",
                             total_qty : { $sum : "$qty" } } },
                         { $match : {qty : { $gte : 1 } } } )
  </code></pre>
  
  <p>Does this make sense?</p>
  
  <p>Thanks in advance to anyone.</p>