title: How to reduce aggregration time mongo db
body: <p>I have written this query to aggregate data</p>
  <pre><code>db.attendancesCollection.aggregate([
     {
        '$match': {
           'Date': { &quot;$gte&quot;: start_date, &quot;$lte&quot;: end_date },
           'Location': found_location[&quot;locationName&quot;],
           'locationId': { '$exists': True },
           'workerType': workerType
        }
     },
     {
        &quot;$group&quot;: {
           '_id': {
              'employeeId': '$employeeId',
              'workerId': '$workerId',
              'workerFullName': '$workerFullName'
           },
           'dailyPointsArray': {
              '$push': {
                 'Date': '$Date',
                 'createdAs': '$createdAs',
                 'Points': '$shiftPoints',
                 'inTime': '$inTime',
                 'outTime': '$outTime',
              }
           },
           'total_shift_points': { '$sum': '$shiftPoints' },
           'total_duration': { '$sum': '$duration' },
        }
     },
     {
        '$addFields': {
           'Location': found_location[&quot;locationName&quot;],
        }
     },
     {
        '$lookup':
           {
              'from': &quot;users&quot;,
              'localField': &quot;_id.employeeId&quot;,
              'foreignField': &quot;_id&quot;,
              'as': &quot;userInfo&quot;
           }
     }
  ])
  </code></pre>
  <p>There is no issue in running this query, I am getting desired data, but the problem lies in the execution time of the query, it takes about 20-30 secs for that, there are about 500,000 documents in  attendance collection where I am performing aggregation &amp; 1000 docs in user Collection where I am performing a lookup, what can I do to reduce execution time</p>
  <p>This is sample structure</p>
  <pre><code>{
      &quot;_id&quot;: {
          &quot;$oid&quot;: &quot;60dd7d723fcb2eb7df248af3&quot;
      },
      &quot;workerId&quot;: &quot;xxxx&quot;,
      &quot;workerFullName&quot;: &quot;ARUN&quot;,
      &quot;workerType&quot;: &quot;Employee&quot;,
      &quot;workerAadharCardNumber&quot;: &quot;xxxxxxxxxx&quot;,
      &quot;Date&quot;: {
          &quot;$date&quot;: &quot;2021-07-01T00:00:00.000Z&quot;
      },
      &quot;employeeId&quot;: {
          &quot;$oid&quot;: &quot;60dd77c14524e6c116e1692c&quot;
      },
      &quot;Location&quot;: &quot;HEAD OFFICE&quot;,
      &quot;sourceUnitType&quot;: null,
      &quot;duration&quot;: null,
      &quot;shiftPoints&quot;: null,
      &quot;createdAs&quot;: &quot;ABSENT&quot;,
      &quot;ruleApplied&quot;: null,
      &quot;detections&quot;: [],
      &quot;locationId&quot;: {
          &quot;$oid&quot;: &quot;60dd6d303da6c17209d5ef68&quot;
      },
      &quot;workerFaceRegistered&quot;: true
  }
  </code></pre>