Mongo Playground

Configuration

[{_id:1,employeeId:"50052196",name:"abc",lastUpdatedOn:ISODate("2022-01-10T10:25:38.859+00:00"),},{_id:2,employeeId:"50052197",name:"xyz",lastUpdatedOn:ISODate("2022-01-17T10:25:38.859+00:00"),},{_id:3,employeeId:"50052198",name:"pqr",lastUpdatedOn:ISODate("2022-01-17T10:25:38.859+00:00"),},{_id:4,employeeId:"50052196",name:"abc",lastUpdatedOn:ISODate("2022-01-12T11:30:58.435+00:00"),},{_id:5,employeeId:"50052196",name:"abc",lastUpdatedOn:ISODate("2022-01-15T15:45:00.159+00:00"),},]

Query

db.collection.aggregate([{$sort:{lastUpdatedOn:-1}},{$group:{_id:"$employeeId",root:{$first:"$$ROOT"}}},{$replaceRoot:{newRoot:"$root"}},{$out:"collection"}])

Result