Вы можете использовать unwind
агрегат
db.collection.aggregate([
{
$unwind:{
path: "$Records",
includeArrayIndex:"firstUnwind"
}
},
{
$unwind:{
path: "$Records",
}
},
{ $sort : { Match: -1} },
{
$group: {
_id: {
id: "$_id",
firstUnwind: "firstUnwind"
},
Index_Number: {$first: "$Index_Number"},
Records: {$push: "$Records"}
}
},
{
$group: {
_id: "$_id.id",
Index_Number: {$first: "$Index_Number"},
Records: {$push: "$Records"}
}
}
])`