title: MongoDB 2.2 Aggregation Framework group by field name
body: <p>Is it possible to group-by field name? Or do I need a different structure so I can group-by value?</p>
  
  <p>I know we can use group by on values and we can unwind arrays, but is it possible to get total apples, pears and oranges owned by John amongst the three houses here <em>without</em> specifying "apples", "pears" and "oranges" explicitly as part of the query? (so NOT like this); </p>
  
  <pre><code>// total all the fruit John has at each house
  db.houses.aggregate([
      {
          $group: {
              _id: null,
  
              "apples":  { $sum: "$people.John.items.apples" },
              "pears":   { $sum: "$people.John.items.pears" }, 
              "oranges": { $sum: "$people.John.items.oranges" }, 
          }
      },
  ])
  </code></pre>
  
  <p>In other words, can I group-by the first field-name under "items" and get the aggregate sum of apples:104, pears:202 and oranges:306, but also bananas, melons and anything else that might be there? Or do I need to restructure the data into an array of key/value pairs like categories?</p>
  
  <pre><code>db.createCollection("houses");
  db.houses.remove();
  db.houses.insert(
  [
      {
          House: "birmingham",
          categories : [
              {
                  k : "location",
                  v : { d : "central" }
              }
          ],
          people: {
              John: {
                  items: {
                      apples: 2,
                      pears: 1,
                      oranges: 3,
                  }
              },
              Dave: {
                  items: {
                      apples: 30,
                      pears: 20,
                      oranges: 10,
                  },
              },
          },
      },
      {
          House: "London", categories: [{ k: "location", v: { d: "central" } }, { k: "type", v: { d: "rented" } }],
          people: {
              John: { items: { apples: 2, pears: 1, oranges: 3, } },
              Dave: { items: { apples: 30, pears: 20, oranges: 10, }, },
          },
      },
      {
          House: "Cambridge", categories: [{ k: "type", v: { d: "rented" } }],
          people: {
              John: { items: { apples: 100, pears: 200, oranges: 300, } },
              Dave: { items: { apples: 0.3, pears: 0.2, oranges: 0.1, }, },
          },
      },
  ]
  );
  </code></pre>
  
  <p>Secondly, and more importantly, could I then also group by "house.categories.k" ? In other words, is it possible to find out how many "apples" "John" has in "rented" vs "owned" or "friends" houses (so group by "categories.k.type")?</p>
  
  <p>Finally - if this is even possible, is it sensible? At first I thought it was quite useful to create dictionaries of nested objects using actual field names of the object, as it seemed a logical use of a document database, and it seemed to make the MR queries easier to write vs arrays, but now I'm starting to wonder if this is all a bad idea and having variable field names makes it very tricky/inefficient to write aggregation queries.</p>