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] },
},
},
]);