Performance Issue when using $group Pipeline

Additional to Michael’s answer above, since you have your last step has:

{
  condition_count_complated: {
    $eq: 0
  }
}

The 0 is for status NOT IN ["WAITING_FOR_APPROVAL", "APPROVED", "SENT"]. You can use $nin operator for that.

$nin selects the documents where:

  • the specified field value is not in the specified array or
  • the specified field does not exist.

So the first $match stage could directly be:

{
    tenantId: "MY_TENANT_ID",
    status: {
      $nin: ["WAITING_FOR_APPROVAL", "APPROVED", "SENT"]
}

Mongo Playground