Aggregate Query and Sort OPerations

I am trying to sort the data in al alphabectival order but the results keep changing everytime I run the query.
Not sure if that some something to with aggregation or what is going one. Here is my query
db.getCollection(“reportings”).aggregate
([
{
$match: {
createdAt: {
$gte: ISODate(“2022-05-01T22:58:18.987+0000”),
$lte: ISODate(“2022-05-13T22:58:18.987+0000”)
},

          },
        
      },
     
      {
        $group: {
          _id: {
            username: "$username",
          },
          videoSeconds: {
            $push: "$videoSeconds",
          },
          audioSeconds: {
            $push: "$audioSeconds",
          },
          breakSeconds: {
            $push: "$breakSeconds",
          },
          session: {
            $push: {
              $divide: [
                {
                  $dateDiff: {
                    startDate: "$createdAt",
                    endDate: "$exitAt",
                    unit: "second",
                  },
                },
                60,
              ],
            },
          },
          disengagedEmotionCount: {
            $push: "$disengagedEmotionCount",
          },
          engagedEmotionCount: {
            $push: "$engagedEmotionCount",
          },
          lunchSeconds: {
            $push: "$lunchSeconds",
          },
          otherEmotionsCount: {
            $push: "$otherEmotionsCount",
          },
          screenSeconds: {
            $push: "$screenSeconds",
          },
          screenTwoSeconds: {
            $push: "$screenTwoSeconds",
          },
          roomname: {
            $push: "$roomname",
          },
          //Sum of Seconds
          videoSecondsSum: {
            $sum: "$videoSeconds",
          },
          audioSecondsSum: {
            $sum: "$audioSeconds",
          },
          breakSecondsSum: {
            $sum: "$breakSeconds",
          },
          lunchSecondsSum: {
            $sum: "$lunchSeconds",
          },
          screenSecondsSum: {
            $sum: "$screenSeconds",
          },
          screenTwoSecSum: {
            $sum: "$screenTwoSeconds",
          },
          engagedEmotionCountSum: {
            $sum: "$engagedEmotionCount",
          },
          disengagedEmotionCountSum: {
            $sum: "$disengagedEmotionCount",
          },
          otherEmotionsCountSum: {
            $sum: "$otherEmotionsCount",
          },
        },
        
      },
       {$sort:{username:1}},
      //Seconds to Minutes
      {
        $project: {
          videoMint: { $divide: ["$videoSecondsSum", 60] },
          audioMint: { $divide: ["$audioSecondsSum", 60] },
          breakMint: { $divide: ["$breakSecondsSum", 60] },
          lunchMint: { $divide: ["$lunchSecondsSum", 60] },
          screenMint: { $divide: ["$screenSecondsSum", 60] },
          screenTwoMint: { $divide: ["$screenTwoSecSum", 60] },
          roomname: "$roomname",
          session: { $sum: ["$session"] },
          //multiply Count with 5
          engagedEmotionSeconds: {
            $multiply: ["$engagedEmotionCountSum", 5],
          },
          disengagedEmotionSeconds: {
            $multiply: ["$disengagedEmotionCountSum", 5],
          },
          otherEmotionsSeconds: {
            $multiply: ["$otherEmotionsCountSum", 5],
          },
        },
      },
      //Rounding Data
      {
        $project: {
          videoMintTotal: { $round: ["$videoMint", 0] },
          audioMintTotal: { $round: ["$audioMint", 0] },
          breakMintTotal: { $round: ["$breakMint", 0] },
          lunchMintTotal: { $round: ["$lunchMint", 0] },
          screenMintTotal: { $round: ["$screenMint", 0] },
          screenTwoMintTotal: { $round: ["$screenTwoMint", 0] },
          roomname: "$roomname",
          session: "$session",
          //convert count to minutes
          engagedEmotionMint: { $divide: ["$engagedEmotionSeconds", 60] },
          disengagedEmotionMint: {
            $divide: ["$disengagedEmotionSeconds", 60],
          },
          otherEmotionsMint: { $divide: ["$otherEmotionsSeconds", 60] },
        },
      },
      {
        $project: {
          videoMintTotal: "$videoMintTotal",
          audioMintTotal: "$audioMintTotal",
          breakMintTotal: "$breakMintTotal",
          lunchMintTotal: "$lunchMintTotal",
          screenMintTotal: "$screenMintTotal",
          screenTwoMintTotal: "$screenTwoMintTotal",
          roomname: "$roomname",
          session: { $round: ["$session", 0] },
          engagedEmotionMintTotal: { $round: ["$engagedEmotionMint", 0] },
          disengagedEmotionMintTotal: {
            $round: ["$disengagedEmotionMint", 2],
          },
          otherEmotionsMintTotal: { $round: ["$otherEmotionsMint", 0] },
        },
      },
    ]);

You sort with {username:1} but you do not have a field named username after the $group stage.

The field name _id.username after the $group stage.

Make sense. what would be the syntax of the field in the sort.
I tried {$sort:{_id.username:-1}}, but get and error

When using dot notations you need quotes. So try

{ "$sort" : { "_id.username" : -1 } }

That did fix the syntax error , how ever the sory is still not working.
image
Here is the results I am getting so not sure why it would sort it alphabetically.
Also I would amiss if I didnt thank you for helping out

try to sort in the last stage

The field you sorted by was called _id.username - it appears to be called username in your screenshot. Do you have other stages that you didn’t include in the original pipeline you posted?

What are you using to run this aggregation and look at its results? Could the client be the actual problem?

Asya