title: Mongo aggregate query for facets navigation
body: <p>I have a collection <code>products</code> with documents (products) like this (relevant code only):</p>
  
  <pre><code>{ "_id": "prod1", ... "colors": ["red","blue"], "size": ["S", "M"], ...}
  { "_id": "prod2", ... "colors": ["red","green"], "size": ["S", "XL"], ...}
  { "_id": "prod3", ... "colors": ["yellow","blue"], "size": ["XL", "XXL"], ...}
  </code></pre>
  
  <p>I would like to offer the user only the available selection, as a faceted search, like the name of the option and the number of options available.</p>
  
  <p>colors:</p>
  
  <pre><code>red 2
  blue 2
  green 1
  yellow 1
  </code></pre>
  
  <p>sizes:</p>
  
  <pre><code>S 2
  M 1
  XL 2
  XXL 1
  </code></pre>
  
  <p>If I had to do it in SQL, I would probably have to run a query for each facet group.</p>
  
  <pre><code>SELECT colors, count(*) AS number FROM products GROUP BY colors
  </code></pre>
  
  <p>than the second query</p>
  
  <pre><code>SELECT size, count(*) AS number FROM test GROUP BY sizes
  </code></pre>
  
  <p>If somebody chose color "red", than I would probably have to add to each query the "WHERE" clause:</p>
  
  <pre><code>WHERE colors = 'red'
  </code></pre>
  
  <p>Could somebody more experienced than me, help me with this example in MongoDB? Do I have to run two queries or is there something that I am missing and it could be done in one query? I assume I will have a great number of products. Any help appreciated. Especially if there are any tricks to speed it up. I would like to get it right from the beginning, hence asking the question. Thanks.</p>