Database

[{"_id":ObjectId("5f072d45856bc306147c1dcd"),"readOnly":false,"participants":[{"clientType":"support","docId":"","metaData":{"fullName":"Support","email":"Support@mydomain.com","phoneNum":"+1234567890"}},{"clientType":"worker","docId":"5e21c48ee178473be81e032e","metaData":{"fullName":"Rami","email":"myemail@gmail.com","phoneNum":"+1234567890"}}],"messages":[{"id":"IGK-fIt-2zz","sender":"support, ","body":"CONTROL: Agent \"Greendizeriii\" has answered the conversation","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-11T06:33:48.348Z")},"delivered":{"state":false},"seen":{"state":true,"dateTime":ISODate("2020-07-11T06:40:06.851Z")}}},{"id":"L0f-hwj-QUL","sender":"support, ","body":"Hello rami, How can I help you today","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-11T06:35:57.406Z")},"delivered":{"state":false},"seen":{"state":true,"dateTime":ISODate("2020-07-11T06:40:06.851Z")}}},{"id":"n0k-s3u-UmN","sender":"support, ","body":"are you still there ?","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-11T06:36:13.350Z")},"delivered":{"state":false},"seen":{"state":true,"dateTime":ISODate("2020-07-11T06:40:06.851Z")}}}],"controlData":{"isReserved":{"state":false,"reservedUntil":null,"operatorId":null}},"__v":0},{"_id":ObjectId("5f1f143f4134643044638902"),"readOnly":false,"participants":[{"clientType":"support","docId":"","metaData":{"fullName":"Support","email":"Support@mydomain.com","phoneNum":"+1234567890"}},{"clientType":"worker","docId":"5ea9409ff3243f483c2483ed","metaData":{"fullName":"Ronaldo","email":"dummyemail02@gmail.com","phoneNum":"+1234567890"}}],"messages":[{"id":"uto-qQb-0cr","sender":"support, ","body":"CONTROL: Agent \"Greendizeriii\" has answered the conversation","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-27T17:51:59.753Z")},"delivered":{"state":false},"seen":{"state":true,"dateTime":ISODate("2020-07-27T17:52:01.240Z")}}},{"id":"FQg-fSQ-jQ1","sender":"support, ","body":"jjj","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-27T17:51:59.751Z")},"delivered":{"state":false},"seen":{"state":true,"dateTime":ISODate("2020-07-27T17:52:01.240Z")}}},{"id":"Uqm-J4S-LUT","sender":"support, ","body":"CONTROL: Agent \"Greendizeriii\" has left the conversation","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-27T17:53:05.142Z")},"delivered":{"state":false},"seen":{"state":true,"dateTime":ISODate("2020-07-27T17:53:05.558Z")}}},{"id":"gs1-Qqz-jkI","sender":"worker, 5ea9409ff3243f483c2483ed","body":"Hello","commData":{"sent":{"state":true,"dateTime":ISODate("2020-07-25T03:58:50.600Z")},"delivered":{"state":false},"seen":{"state":false}}}],"controlData":{"isReserved":{"state":false,"reservedUntil":null,"operatorId":null}},"__v":0}]

Query

db.collection.aggregate([{$addFields:{lastSent:{$let:{vars:{last:{$arrayElemAt:["$messages",-1]}},in:"$$last.commData.sent.dateTime"}}}},{$sort:{lastSent:1}}])

Result