title: Need to mark paid installments in an array using mongoDB aggregation
body: <p>I have a schema named orders which looks like this : </p>
  
  <pre><code>{
      "_id" : ObjectId("5cd42f7b16c2654ea9138ece"),
      "customerId" : ObjectId("5c8222109146d119ccc5243f"),
      "orderAmount" : NumberInt(10000),
      "paidAmount" : NumberInt(4000),
      "installments" : [
          {
              "dueDate" : ISODate("2020-01-01"),
              "amount" : NumberInt(2000)
          },
          {
              "dueDate" : ISODate("2020-01-07"),
              "amount" : NumberInt(6000)
          },
          {
              "dueDate" : ISODate("2020-01-04"),
              "amount" : NumberInt(2000)
          }
      ]
  }
  </code></pre>
  
  <p>I want to write an aggregation function that sorts the installments according to dueDate and mark them paid according to paidAmount. For example for this case the function should return </p>
  
  <pre><code>{
      "_id" : ObjectId("5cd42f7b16c2654ea9138ece"),
      "customerId" : ObjectId("5c8222109146d119ccc5243f"),
      "orderAmount" : NumberInt(10000),
      "paidAmount" : NumberInt(4000),
      "installments" : [
          {
              "dueDate" : ISODate("2020-01-01"),
              "amount" : NumberInt(2000),
              "paid" : true
          },
          {
              "dueDate" : ISODate("2020-01-04"),
              "amount" : NumberInt(2000),
              "paid" : true
          },
          {
              "dueDate" : ISODate("2020-01-07"),
              "amount" : NumberInt(6000),
              "paid" : false
          }
      ]
  }
  </code></pre>
  
  <p>Now I can sort the array using the $unwind and $sort functions like this:</p>
  
  <pre><code>db.orders.aggregate([
      {$unwind : "$installments"},
      {$sort : {"dueDate" : 1}}
  ]);
  </code></pre>
  
  <p>What I am stuck on is how to group the array back so that it gives me the desired result. I can only use aggregation here. </p>