I want to sort these records in descending order based on their 2021 average rating:
[
{
"id": 1,
"slug": "shop 1",
"stats": {
"time_stats": {
"2021": {
"1": 9.04,
"2": 8.17,
"10": 8.46,
"7": 9.35,
"11": 8.53,
"6": 8.35,
"5": 8.63,
"9": 9.13,
"3": 8.9,
"8": 7.64,
"4": 8.65,
"12": 7.45
},
}
}
},
{
"id": 2,
"slug": "shop 2",
"stats": {
"time_stats": {
"2021": {
"1": 5,
"10": 9.35,
"12": 6,
"11": 8,
"2": 3,
"3": 5,
"9": 4
},
}
}
},
{
"id": 3,
"slug": "shop 3",
"stats": {
"time_stats": {
"2021": {
"3": 9.57,
"11": 7.83,
"4": 8.94,
"2": 9.1,
"6": 8.5,
"5": 9.43,
"12": 7.93,
"10": 9.28,
"9": 9.84,
"7": 9.38,
"1": 8.91,
"8": 9.75
},
}
}
}
]
Each attribute inside 2021 object represents a month. I don't have any idea where to start, Here's what I've tried so far
db.collection.aggregate([
{
$unwind: "$stats"
},
{
$unwind: "$stats.time_stats.2021"
},
{
$group: {
_id: "$slug",
yearAvg: {
$avg: {
$sum: [
"$stats.time_stats.2021.1",
"$stats.time_stats.2021.2",
"$stats.time_stats.2021.3",
"$stats.time_stats.2021.4",
"$stats.time_stats.2021.5",
"$stats.time_stats.2021.6",
"$stats.time_stats.2021.7",
"$stats.time_stats.2021.8",
"$stats.time_stats.2021.9",
"$stats.time_stats.2021.10",
"$stats.time_stats.2021.11",
"$stats.time_stats.2021.12",
]
}
}
}
}
])
but I'm not sure how to proceed with the implementation. Any help is much appreciated!