title: How to group in mongo db on dynamic attributes with the aggregation framework?
body: <p>I am using the following schema from the mongodb <a href="http://docs.mongodb.org/manual/faq/indexes/#how-can-i-effectively-use-indexes-strategy-for-attribute-lookups" rel="nofollow">FAQ</a>:</p>
  
  <pre><code>{
    attrib : [
          { k: "color", v: "red" },
          { k: "shape", v: "rectangle" },
          { k: "color", v: "blue" },
          { k: "avail", v: true }
         ]
  }
  </code></pre>
  
  <p>How can I group and count the collection e.g. by "color"? I would prefer using the aggregation framework if possible instead of map reduce.</p>
  
  <p>My result should look like this:</p>
  
  <pre><code>[
      {
          v: "blue",
          count: 5
      },
      {
          v: "red",
          count: 2
      },
      {
          v: "black",
          count: 52
      }
  ]
  </code></pre>