Database

[{_id:1,company:"companyA",endTime:new Date("2022-01-01T00:00:00Z")},{_id:2,company:"companyB",endTime:new Date("2022-02-11T00:00:00Z")},{_id:3,company:"companyA",endTime:new Date("2022-01-03T00:00:00Z")},{_id:4,company:"companyB",endTime:new Date("2022-01-04T00:00:00Z")},{_id:5,company:"companyA",endTime:new Date("2022-02-21T00:00:00Z")},{_id:6,company:"companyB",endTime:new Date("2022-01-01T00:00:00Z")},]

Query

db.collection.aggregate([{$sort:{endTime:-1}},{$group:{_id:"$company",data:{$first:"$$ROOT"}}},{"$replaceWith":"$data"}])

Result