Here is the structure of my collection:
{
"_id" : "Pd2fl7xcT3iWEmpAafv4DA",
"slot" : 1,
"stat" : [
{
"unitStat" : "5"
"value" : 13
},
{
"unitStat" : "18",
"value" : 1.96
},
{
"unitStat" : "28",
"value" : 1373
},
{
"unitStat" : "41",
"roll" : 2,
"value" : 69
}
]
}
I am interested in retrieving the top 5 sorted objects (based on any unitStat
) for each slot.
Initially, I attempted to make individual calls to the database for each slot, but this approach proved inefficient. Subsequently, I experimented with aggregation for a single slot:
db.collection.aggregate(
{
`$match`: {
slot: 1,
secondaryStat: {
`$elemMatch`: {
unitStat:'5'
}
}
}
},
{
`$unwind`: `'$secondaryStat'`
},
{
`$match`: {
'secondaryStat.unitStat' : '5'
}
},
{
`$sort`: {
'secondaryStat.value': -1
}
},
{
`$limit`: 5
}
)
Is it possible to retrieve the top 5 sorted objects from a combination of 6 different slots?