How to get individual counts when grouping two fields at the time?

Hi Team,
I am so worried about getting individual agent counts from collections in that collections there is huge documents, each document has status , priority from this collection How to get each agent_id had how many cases are in the status of Open, Closed, Pending . And Priority has Low , Medium, High, Escalated counts (Which not in Closed status) of each individual agent_id … I was tried below query

db.case_details.aggregate( [
  {
    $facet: {
      "status_counts": [{"$group" : {_id:{source:"$assigned_to_ref.$id",status:"$status"}, count:{$sum:1}}}],
      "Priority_counts": [{$match:{"status":{$ne:"Closed"}}},{"$group" : {_id:{agent_id:"$assigned_to_ref.$id",Priority:"$priority"}, count:{$sum:1}}}]
          }
        }
      ])

Getting result from the above query

{
    "status_counts" : [
        {
            "_id" : {
                "agent_id" : "61a4740aaa59a81392928a7d",
                "status" : "Closed"
            },
            "count" : 30.0
        },
        {
            "_id" : {
                "agent_id" : "61a4740aaa59a81392928a7d",
                "status" : "Pending"
            },
            "count" : 88.0
        },
        {
            "_id" : {
                "agent_id" : "61a4732eaa59a81392928a7c",
                "status" : "Pending"
            },
            "count" : 20.0
        },
        {
            "_id" : {
                "agent_id" : "6184fdcc68b51a7ad0cff7a9",
                "status" : "Pending"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a4780baa59a81392928a89",
                "status" : "Closed"
            },
            "count" : 94.0
        },
        {
            "_id" : {
                "agent_id" : "61a47cbeaa59a81392928a8e",
                "status" : "Closed"
            },
            "count" : 143.0
        },
        {
            "_id" : {
                "status" : "Open"
            },
            "count" : 222.0
        },
        {
            "_id" : {
                "agent_id" : "61a47d13aa59a81392928a8f",
                "status" : "Closed"
            },
            "count" : 120.0
        },
        {
            "_id" : {
                "agent_id" : "61a47e69aa59a81392928a92",
                "status" : "Closed"
            },
            "count" : 97.0
        },
        {
            "_id" : {
                "agent_id" : "61a47696aa59a81392928a85",
                "status" : "Pending"
            },
            "count" : 146.0
        },
        {
            "_id" : {
                "agent_id" : "61a47d13aa59a81392928a8f",
                "status" : "Pending"
            },
            "count" : 154.0
        },
        {
            "_id" : {
                "agent_id" : "61a47836aa59a81392928a8a",
                "status" : "Closed"
            },
            "count" : 29.0
        },
        {
            "_id" : {
                "agent_id" : "61a4732eaa59a81392928a7c",
                "status" : "Closed"
            },
            "count" : 11.0
        },
        {
            "_id" : {
                "agent_id" : "61a474f7aa59a81392928a80",
                "status" : "Open"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a475d8aa59a81392928a82",
                "status" : "Pending"
            },
            "count" : 79.0
        },
        {
            "_id" : {
                "agent_id" : "61a0ad9b8fc5a9742c9cf619",
                "status" : "Open"
            },
            "count" : 2.0
        },
        {
            "_id" : {
                "agent_id" : "61a47659aa59a81392928a84",
                "status" : "Pending"
            },
            "count" : 155.0
        },
        {
            "_id" : {
                "agent_id" : "619b71ec6ec78556320d8e27",
                "status" : "Open"
            },
            "count" : 9.0
        },
        {
            "_id" : {
                "agent_id" : "61a47696aa59a81392928a85",
                "status" : "Closed"
            },
            "count" : 93.0
        },
        {
            "_id" : {
                "agent_id" : "61a452f467dacd4a141d195e",
                "status" : "Pending"
            },
            "count" : 87.0
        },
        {
            "_id" : {
                "agent_id" : "61a4533d67dacd4a141d195f",
                "status" : "Closed"
            },
            "count" : 149.0
        },
        {
            "_id" : {
                "agent_id" : "61a47468aa59a81392928a7e",
                "status" : "Closed"
            },
            "count" : 130.0
        },
        {
            "_id" : {
                "agent_id" : "61a474abaa59a81392928a7f",
                "status" : "Closed"
            },
            "count" : 26.0
        },
        {
            "_id" : {
                "agent_id" : "61a4533d67dacd4a141d195f",
                "status" : "Pending"
            },
            "count" : 158.0
        },
        {
            "_id" : {
                "agent_id" : "61a476e1aa59a81392928a86",
                "status" : "Open"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a452f467dacd4a141d195e",
                "status" : "Closed"
            },
            "count" : 152.0
        },
        {
            "_id" : {
                "agent_id" : "61a47c5aaa59a81392928a8d",
                "status" : "Closed"
            },
            "count" : 220.0
        },
        {
            "_id" : {
                "agent_id" : "61a47c5aaa59a81392928a8d",
                "status" : "Pending"
            },
            "count" : 103.0
        },
        {
            "_id" : {
                "agent_id" : "61a47dc0aa59a81392928a90",
                "status" : "Pending"
            },
            "count" : 99.0
        },
        {
            "_id" : {
                "agent_id" : "61a4780baa59a81392928a89",
                "status" : "Pending"
            },
            "count" : 158.0
        },
        {
            "_id" : {
                "agent_id" : "61a477bfaa59a81392928a88",
                "status" : "Pending"
            },
            "count" : 283.0
        },
        {
            "_id" : {
                "agent_id" : "61a8fcdc6e4376427f0ccb1b",
                "status" : "Pending"
            },
            "count" : 2.0
        },
        {
            "_id" : {
                "agent_id" : "61a47747aa59a81392928a87",
                "status" : "Pending"
            },
            "count" : 5.0
        },
        {
            "_id" : {
                "agent_id" : "61a47747aa59a81392928a87",
                "status" : "Closed"
            },
            "count" : 162.0
        },
        {
            "_id" : {
                "agent_id" : "61a47659aa59a81392928a84",
                "status" : "Closed"
            },
            "count" : 169.0
        },
        {
            "_id" : {
                "agent_id" : "61a47468aa59a81392928a7e",
                "status" : "Pending"
            },
            "count" : 113.0
        },
        {
            "_id" : {
                "agent_id" : "61a476e1aa59a81392928a86",
                "status" : "Pending"
            },
            "count" : 192.0
        },
        {
            "_id" : {
                "agent_id" : "61a47c5aaa59a81392928a8d",
                "status" : "Open"
            },
            "count" : 33.0
        },
        {
            "_id" : {
                "agent_id" : "61a47564aa59a81392928a81",
                "status" : "Closed"
            },
            "count" : 80.0
        },
        {
            "_id" : {
                "agent_id" : "61a474abaa59a81392928a7f",
                "status" : "Open"
            },
            "count" : 2.0
        },
        {
            "_id" : {
                "agent_id" : "6184fe3068b51a7ad0cff7aa",
                "status" : "Closed"
            },
            "count" : 3.0
        },
        {
            "_id" : {
                "agent_id" : "61a47e13aa59a81392928a91",
                "status" : "Pending"
            },
            "count" : 128.0
        },
        {
            "_id" : {
                "agent_id" : "61a475d8aa59a81392928a82",
                "status" : "Closed"
            },
            "count" : 263.0
        },
        {
            "_id" : {
                "agent_id" : "61a47e69aa59a81392928a92",
                "status" : "Pending"
            },
            "count" : 54.0
        },
        {
            "_id" : {
                "agent_id" : "61a47dc0aa59a81392928a90",
                "status" : "Closed"
            },
            "count" : 160.0
        },
        {
            "_id" : {
                "agent_id" : "61a47dc0aa59a81392928a90",
                "status" : "Open"
            },
            "count" : 31.0
        },
        {
            "_id" : {
                "agent_id" : "61a47468aa59a81392928a7e",
                "status" : "Open"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a4761faa59a81392928a83",
                "status" : "Pending"
            },
            "count" : 135.0
        },
        {
            "_id" : {
                "agent_id" : "61a4761faa59a81392928a83",
                "status" : "Closed"
            },
            "count" : 55.0
        },
        {
            "_id" : {
                "agent_id" : "61a47836aa59a81392928a8a",
                "status" : "Pending"
            },
            "count" : 167.0
        },
        {
            "_id" : {
                "agent_id" : "61a47564aa59a81392928a81",
                "status" : "Pending"
            },
            "count" : 186.0
        },
        {
            "_id" : {
                "agent_id" : "619b71ec6ec78556320d8e27",
                "status" : "Closed"
            },
            "count" : 68.0
        },
        {
            "_id" : {
                "agent_id" : "61a474abaa59a81392928a7f",
                "status" : "Pending"
            },
            "count" : 21.0
        },
        {
            "_id" : {
                "agent_id" : "61a47e13aa59a81392928a91",
                "status" : "Closed"
            },
            "count" : 87.0
        },
        {
            "_id" : {
                "agent_id" : "61a476e1aa59a81392928a86",
                "status" : "Closed"
            },
            "count" : 14.0
        },
        {
            "_id" : {
                "agent_id" : "6184fdcc68b51a7ad0cff7a9",
                "status" : "Closed"
            },
            "count" : 55793.0
        },
        {
            "_id" : {
                "agent_id" : "61a47cbeaa59a81392928a8e",
                "status" : "Pending"
            },
            "count" : 159.0
        },
        {
            "_id" : {
                "agent_id" : "619b71ec6ec78556320d8e27",
                "status" : "Pending"
            },
            "count" : 194.0
        },
        {
            "_id" : {
                "agent_id" : "61a477bfaa59a81392928a88",
                "status" : "Closed"
            },
            "count" : 45.0
        }
    ],
    "Priority_counts" : [
        {
            "_id" : {
                "agent_id" : "61a47e69aa59a81392928a92",
                "Priority" : "Escalated"
            },
            "count" : 54.0
        },
        {
            "_id" : {
                "Priority" : "Escalated"
            },
            "count" : 202.0
        },
        {
            "_id" : {
                "agent_id" : "61a475d8aa59a81392928a82",
                "Priority" : "Escalated"
            },
            "count" : 79.0
        },
        {
            "_id" : {
                "Priority" : "High"
            },
            "count" : 5.0
        },
        {
            "_id" : {
                "agent_id" : "61a47dc0aa59a81392928a90",
                "Priority" : "Low"
            },
            "count" : 5.0
        },
        {
            "_id" : {
                "agent_id" : "61a47836aa59a81392928a8a",
                "Priority" : "Escalated"
            },
            "count" : 167.0
        },
        {
            "_id" : {
                "agent_id" : "6184fdcc68b51a7ad0cff7a9",
                "Priority" : "High"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a4761faa59a81392928a83",
                "Priority" : "Escalated"
            },
            "count" : 135.0
        },
        {
            "_id" : {
                "agent_id" : "61a47dc0aa59a81392928a90",
                "Priority" : "Escalated"
            },
            "count" : 124.0
        },
        {
            "_id" : {
                "agent_id" : "61a47dc0aa59a81392928a90",
                "Priority" : "Medium"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a4533d67dacd4a141d195f",
                "Priority" : "Escalated"
            },
            "count" : 158.0
        },
        {
            "_id" : {
                "agent_id" : "61a47e13aa59a81392928a91",
                "Priority" : "Escalated"
            },
            "count" : 128.0
        },
        {
            "_id" : {
                "agent_id" : "61a4780baa59a81392928a89",
                "Priority" : "Escalated"
            },
            "count" : 158.0
        },
        {
            "_id" : {
                "agent_id" : "61a47c5aaa59a81392928a8d",
                "Priority" : "Medium"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a8fcdc6e4376427f0ccb1b",
                "Priority" : "Escalated"
            },
            "count" : 2.0
        },
        {
            "_id" : {
                "Priority" : "Medium"
            },
            "count" : 7.0
        },
        {
            "_id" : {
                "agent_id" : "61a47cbeaa59a81392928a8e",
                "Priority" : "Escalated"
            },
            "count" : 159.0
        },
        {
            "_id" : {
                "agent_id" : "61a474f7aa59a81392928a80",
                "Priority" : "Escalated"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a452f467dacd4a141d195e",
                "Priority" : "Escalated"
            },
            "count" : 87.0
        },
        {
            "_id" : {
                "agent_id" : "61a47c5aaa59a81392928a8d",
                "Priority" : "Low"
            },
            "count" : 1.0
        },
        {
            "_id" : {
                "agent_id" : "61a47747aa59a81392928a87",
                "Priority" : "Escalated"
            },
            "count" : 5.0
        },
        {
            "_id" : {
                "agent_id" : "61a477bfaa59a81392928a88",
                "Priority" : "Escalated"
            },
            "count" : 283.0
        },
        {
            "_id" : {
                "agent_id" : "61a4740aaa59a81392928a7d",
                "Priority" : "Escalated"
            },
            "count" : 88.0
        },
        {
            "_id" : {
                "agent_id" : "61a0ad9b8fc5a9742c9cf619",
                "Priority" : "Escalated"
            },
            "count" : 2.0
        },
        {
            "_id" : {
                "Priority" : "Low"
            },
            "count" : 8.0
        },
        {
            "_id" : {
                "agent_id" : "61a476e1aa59a81392928a86",
                "Priority" : "Escalated"
            },
            "count" : 193.0
        },
        {
            "_id" : {
                "agent_id" : "61a47696aa59a81392928a85",
                "Priority" : "Escalated"
            },
            "count" : 146.0
        },
        {
            "_id" : {
                "agent_id" : "619b71ec6ec78556320d8e27",
                "Priority" : "Escalated"
            },
            "count" : 203.0
        },
        {
            "_id" : {
                "agent_id" : "61a4732eaa59a81392928a7c",
                "Priority" : "Escalated"
            },
            "count" : 20.0
        },
        {
            "_id" : {
                "agent_id" : "61a47468aa59a81392928a7e",
                "Priority" : "Escalated"
            },
            "count" : 114.0
        },
        {
            "_id" : {
                "agent_id" : "61a474abaa59a81392928a7f",
                "Priority" : "Escalated"
            },
            "count" : 23.0
        },
        {
            "_id" : {
                "agent_id" : "61a47659aa59a81392928a84",
                "Priority" : "Escalated"
            },
            "count" : 155.0
        },
        {
            "_id" : {
                "agent_id" : "61a47d13aa59a81392928a8f",
                "Priority" : "Escalated"
            },
            "count" : 154.0
        },
        {
            "_id" : {
                "agent_id" : "61a47564aa59a81392928a81",
                "Priority" : "Escalated"
            },
            "count" : 186.0
        },
        {
            "_id" : {
                "agent_id" : "61a47c5aaa59a81392928a8d",
                "Priority" : "Escalated"
            },
            "count" : 134.0
        }
    ]
}

But I am expecting Each Agent_id counts at one place…
Expected Output:

>{
 "agent_id" : "61a47c5aaa59a81392928a8d",
  Total : 62, 
  Open: 20 ,
  Pending: 12,
  Closed: 30,
  Low : 15,
  High: 10,
  Medium : 3,
  Escalated : 4 
},
{
 "agent_id" : "61a47c5aaa59a81392928a8c",
  Total : 62, 
  Open: 20 ,
  Pending: 12,
  Closed: 30,
  Low : 15,
  High: 10,
  Medium : 3,
  Escalated : 4
 }

Please help me on this …

Forgot change as agent_id insted of source

To get one document per agent_id you need a new $group with _id:$_id.agent_id. You would use the $push accumulator with { status:$_id.status , count:$count}.

The format will not be exactly what you want but the data will grouped like you wish. You could use $project with $arrayToObject to get your final format. Personally, I do this type of data cosmetics on the application side.