title: How to transform array to objects in mongodb/mongoose aggregation?
body: <p>There are three collections.</p>
  <p><strong>Leave</strong></p>
  <ul>
  <li>_id</li>
  <li>user (ref to user model)</li>
  <li>status</li>
  </ul>
  <p><strong>LeaveApproval</strong></p>
  <ul>
  <li>_id</li>
  <li>leave (ref to leave model)</li>
  <li>auditUser (ref to user model)</li>
  <li>role</li>
  </ul>
  <p><strong>User</strong></p>
  <ul>
  <li>_id</li>
  <li>name</li>
  </ul>
  <p>After performing lookup stage on Leave collection:</p>
  <pre><code>Leave.aggregate([
      $lookup: {
          from: 'leaveapprovals',
          localField: '_id',
          foreignField: 'leave',
          as: 'approverRecommender'
      }
  ])
  </code></pre>
  <pre><code>{
      _id: ObjectId('6262851ab059b81fd9076532'),
      user: ObjectId('21ca9490192add08fa385aec'),
      status: &quot;pending&quot;,
      approverRecommender: [
          {
              _id: ObjectId('6262851ab059b81fd907653d'),
              leave: ObjectId('6262851ab059b81fd9076532'),
              auditUser: ObjectId('61ca9490192add08fa385aeb'),
              role: &quot;recommender&quot;    
          },
          {
              _id: ObjectId('6262851ab059b81fd907653d'),
              leave: ObjectId('6262851ab059b81fd9076532'),
              auditUser: ObjectId('71ca9490192add08fa385aec'),
              role: &quot;approver&quot;    
          }
      ]
  }
  </code></pre>
  <p>The result that I want to get is:</p>
  <pre><code>{
      _id: ObjectId('6262851ab059b81fd9076532'),
      user: ObjectId('21ca9490192add08fa385aec'),
      status: &quot;pending&quot;,
      recommender: {
          _id: ObjectId('61ca9490192add08fa385aeb'),
          name: &quot;Andrew&quot;
      },
      approver: {
          _id: ObjectId('71ca9490192add08fa385aec'),
          name: &quot;James&quot;
      }
  }
  </code></pre>
  <p>How can I transform it?</p>