Database

[{"_id":"Object1","created":{"$date":"2021-11-14T10:58:01.456Z"},"primaryId":"SomeId1","secondaryIdx":0},{"_id":"Object2","created":{"$date":"2021-11-13T10:58:01.456Z"},"primaryId":"SomeId2","secondaryIdx":0},{"_id":"Object3","created":{"$date":"2021-11-15T10:58:01.456Z"},"primaryId":"SomeId2","secondaryIdx":1},{"_id":"Object4","created":{"$date":"2021-11-16T10:58:01.456Z"},"primaryId":"SomeId1","secondaryIdx":0}]

Query

db.collection.aggregate([{$sort:{primaryId:1,secondaryIdx:1,created:-1}},{$group:{_id:{primaryId:"$primaryId",secondaryIdx:"$secondaryIdx"},lastDoc:{$first:"$$ROOT"}}},{"$replaceRoot":{"newRoot":"$lastDoc"}}])

Result