title: MongoDB slow $unwind and $group aggregation
body: <p>im having some trouble with my MongoDB aggregation. I have documents like:</p>
  <pre><code>{
      _id: &quot;123456&quot;,
      values: [
          {
              &quot;value&quot;: &quot;A&quot;,
              &quot;begin&quot;:0,
              &quot;end&quot;:1,
          }
          {
              &quot;value&quot;: &quot;B&quot;,
              &quot;begin&quot;:1,
              &quot;end&quot;:2,
          }
          {
              &quot;value&quot;: &quot;C&quot;,
              &quot;begin&quot;:3,
              &quot;end&quot;:7,
          }
      ],
      &quot;name&quot;: &quot;test&quot;
  }
  </code></pre>
  <p>And i want to count only the &quot;value&quot; in &quot;values&quot;.
  With some help i got the following aggregation:</p>
  <pre><code>db.collection.aggregate([
    {$unwind: &quot;$values&quot;},
    {$group: {_id: &quot;$values.value&quot;, count: {$sum: 1}}}
  ])
  </code></pre>
  <p>The problem is: it takes me about 20 seconds to get the result for 6k documents.
  Is there anything i can do for optimication?</p>
  <p>Greetings</p>