Mongo Playground

Configuration

[{"department":"HR","employees":20.0},{"department":"Finance","employees":30.0},{"department":"Sales","employees":5.0},{"department":"IT","employees":50.0}]

Query

db.collection.aggregate([{$sort:{department:1}},{$group:{_id:null,employees:{$sum:"$employees"},all_departments:{$push:"$department"}}},{$set:{last_departments:{$concatArrays:[[{$arrayElemAt:["$all_departments",-1]}],[{$arrayElemAt:["$all_departments",-2]}]]}}}])

Result