score:1

The following should get you what you want:

db.collection.aggregate({
    $unwind: "$nodes" // flatten the "nodes" array
}, {
    $sort: { "nodes.count": -1 } // sort descending by "count"
}, {
    $group: { // create the original structure again - just with sorted array elements
        _id: "$_id",
        nodes: { "$push": "$nodes" }
    }
}, {
    $addFields: {
        "nodes": {
            $zip: { // zip two arrays together
                inputs: [
                    "$nodes", // the first one being the existing and now sorted "nodes" array
                    { $range: [ 1, 6 ] } // and the second one being [ 1, 2, 3, 4, 5 ]
                ],
                useLongestLength: true // do not stop after five elements but instead continue using a "null" value
            }
        }
    }
}, {
    $addFields: {
        "nodes": {
            $map: { // transform the "nodes" array
                input: "$nodes",
                as: "this",
                in: {
                    $mergeObjects: [ // by merging two objects
                        { $arrayElemAt: [ "$$this", 0] }, // the first sits at array position 0
                        { 
                            topn: { $arrayElemAt: [ "$$this", 1] } // the second will be a new entity witha a "topn" field holding the second element in the array
                        }
                    ]
                }
            }
        }
    }
})

Related Query