title: How to build aggregation pipeline to receive data in this certain way?
body: <p>I got a MongoDB collection looking like this:</p>
  <pre class="lang-js prettyprint-override"><code>const data = [
    { name: &quot;eggplant&quot;, price: 3.42, quantity: 1},
    { name: &quot;eggplant&quot;, price: 3.17, quantity: 3},
    { name: &quot;potato&quot;, price: 2.12, quantity: 5},
    { name: &quot;potato&quot;, price: 1.99, quantity: 10},
    { name: &quot;eggplant&quot;, price: 3.33, quantity: 3},
    { name: &quot;cucumber&quot;, price: 5.02, quantity: 4},
    { name: &quot;lettuce&quot;, price: 3.42, quantity: 1.5},
    { name: &quot;cucumber&quot;, price: 4.45, quantity: 4},
  ]
  </code></pre>
  <p>And I want to filter this data using aggregation framework on my server, to receive one document of a kind by name, and calculate other fields.
  Basically I need to calculate average price of each specific item, and total quantity.
  So I need somehow to summarize price of each specific item, divide it by the number of documents with that name and set it to new field, and also take the sum of quantity.
  So I want to have data like this in my output:</p>
  <pre class="lang-js prettyprint-override"><code>[
    { name: &quot;eggplant&quot;, averagePrice: 3.30, quantitySum: 7},
    { name: &quot;potato&quot;, averagePrice: 2.05, quantitySum: 15},
    { name: &quot;cucumber&quot;, averagePrice: 4.73, quantitySum: 8},
    { name: &quot;lettuce&quot;, averagePrice: 3.42, quantity: 1.5},
  ]
  </code></pre>
  <p>My question is that are there in aggregation framework any special operator which could help me with that in easy way and I could dive into? Is it even possible to do such calculation in aggregation framework?</p>