I have a student collection that looks like this: How to find a maximum of two exam scores for every student
[
{
"_id" : ObjectId("61868aa03b2fe72b58c891a5"),
"name" : "Max",
"examScores" : [
{
"difficulty" : 4,
"score" : 57.9
},
{
"difficulty" : 6,
"score" : 62.1
},
{
"difficulty" : 3,
"score" : 88.5
}
]
},
{
"_id" : ObjectId("61868aa03b2fe72b58c891a6"),
"name" : "Manu",
"examScores" : [
{
"difficulty" : 7,
"score" : 52.1
},
{
"difficulty" : 2,
"score" : 74.3
},
{
"difficulty" : 5,
"score" : 53.1
}
]
}
]
I want to use aggregation to return the two highest examScores.score
like this:
[
{
name: "Max",
maxExams: [88.5, 62.1]
},
{
name: "Manu",
maxExams: [74.3, 53.1]
}
]
I am tried the aggregation stage $project and $unwind and $sort but all of them fail to solve my problem