Database

[{"_id":"7","at":"2022-11-19 10:00:00","areaId":3,"data":[{"name":"a","sec":34,"x":10.3,"y":23.3},{"name":"a","sec":36,"x":10.3,"y":23.3},{"name":"b","sec":37,"x":10.3,"y":23.3}]},{"_id":"5","at":ISODate("2022-11-19T10:01:00.000Z"),"areaId":3,"data":[{"name":"a","sec":10,"x":10.3,"y":23.3},{"name":"b","sec":12,"x":10.3,"y":23.3}]},{"_id":"8","at":ISODate("2022-11-19T10:02:00.000Z"),"areaId":3,"data":[{"name":"c","sec":10,"x":10.3,"y":23.3},{"name":"b","sec":12,"x":10.3,"y":23.3}]}]

Query

db.collection.aggregate([{$match:{data:{$elemMatch:{name:"a"}}}},{$sort:{at:-1}},{$limit:1},{$project:{res:{$dateAdd:{startDate:"$at",unit:"second",amount:{$reduce:{input:"$data",initialValue:0,in:{$cond:[{$eq:["$$this.name","a",]},{$max:["$$this.sec","$$value"]},"$$value"]}}}}}}}])

Result