title: MongoDB aggregation sum untill with condition (if/else) OR count price and quantity problem
body: <blockquote>
    <p>THE PROBLEM</p>
  </blockquote>
  
  <p>I'm writing a function that could calculated <code>avg price</code> and entire <code>value</code> based on requested <code>quantity</code>, for example:</p>
  
  <pre><code>async funcName (first_100) {
          let market_quantity = await auctions_db.aggregate([
              {
                  $match: {
                      item: 9999,
                  }
              },
              {
                  $sort: {
                      price: 1
                  }
              },
              //RESULT
          ]);
  }
  </code></pre>
  
  <blockquote>
    <p>//RESULT</p>
  </blockquote>
  
  <p>At this point I have following docs:</p>
  
  <pre><code>{
      item_id: 9999,
      price: 1..Number
      quantity: 1..200
      value: price x quantity //it's not virtual field
  },{
      another doc...
  }
  </code></pre>
  
  <p>As I mentioned above the thing that I want it to, is to <code>$sum</code> <code>price</code> field until <code>quantity</code> won't hit 100 or 100+ (or any other number depends on function argument). </p>
  
  <p><em>I don't need to <code>$sum</code> first 100 docs (which are sorted by <code>price</code> ascending, I would use <code>.limit</code> in that case)</em></p>
  
  <p>So it's obvious I have two ways to do so. As for now I'm using <code>collection.find({condition}).cursor()</code> with the same condition and iterate doc over doc in separate <code>for loop</code>, but I know (actually a friend told me) that <em>MongoDB</em> could do the same via <code>aggregate</code> stage via <code>$cond</code> (if/else) block. </p>
  
  <p>Actually I guess that the next <code>$operator</code> after <code>//Result</code> in that case should be <code>$group</code> stage, like this:</p>
  
  <pre><code>            {
                  $group: {
                      _id: "$lastModified",
                      quantity: {$sum: {$cond: if/else }" $quantity"},
                      if (below 100) {$sum: "$value" }
                  }
              }
  </code></pre>
  
  <p>But I have no idea how to do it. So can anyone provide me an example of that? 
  Or there is no difference between <code>aggregate</code> stage and <code>.find({}).cursor</code> and manual iteration?</p>