Это query
для вас.
Протестировано на MongoDB GUI
db.getCollection("chat").aggregate([
{ $match: { sender_id: "user1" } },
{
$group: {
_id: "$receiver_id",
date_message: { $push: { date: "$date", message: "$message" } },
maxDate: { $max: "$date" }
}
},
{
$addFields: {
date_message: {
$arrayElemAt: [
{
$filter: {
input: "$date_message",
as: "message",
cond: { $eq: ["$$message.date", "$maxDate"] }
}
},
0
]
}
}
},
{
$project: {
_id: 1,
date: "$date_message.date",
message: "$date_message.message"
}
}
]);