Slow query on 10 millions records of data when applying grouping aggregation

Check the output for the following

countDocuments({
      manager_id: 4,
      day_start: {
        $gte: ISODate("2021-03-20"),
        $lte: ISODate("2025-12-25"),
      })

If this is 3740444, then that is just how many documents match the query. Your index is just helping the database to locate these documents quicker.

To improve performance you would need to be more selective in your query.

1 Like