Data:
[
{
"_id": ObjectId("100000000000000000000000"),
"from": ObjectId("100000000000000000000000"),
"to": ObjectId("200000000000000000000000"),
"date": ISODate("2021-09-03T11:23:25.184Z"),
"messageContent": "111"
},
{
"_id": ObjectId("200000000000000000000000"),
"from": ObjectId("200000000000000000000000"),
"to": ObjectId("100000000000000000000000"),
"date": ISODate("2021-09-02T11:23:25.184Z"),
"messageContent": "222"
},
{
"_id": ObjectId("300000000000000000000000"),
"from": ObjectId("300000000000000000000000"),
"to": ObjectId("100000000000000000000000"),
"date": ISODate("2021-09-04T11:23:25.184Z"),
"messageContent": "333"
},
{
"_id": ObjectId("400000000000000000000000"),
"from": ObjectId("300000000000000000000000"),
"to": ObjectId("400000000000000000000000"),
"date": ISODate("2021-09-05T11:23:25.184Z"),
"messageContent": "444"
}
]
Aggregate: (you are ObjectId("100000000000000000000000"))
db.collection.aggregate([
{
$match: {
"$or": [
{ "from": ObjectId("100000000000000000000000") },
{ "to": ObjectId("100000000000000000000000") }
]
}
},
{
$project: {
whom: {
$first: {
$filter: {
input: [ "$from", "$to" ],
as: "i",
cond: { $ne: [ "$$i", ObjectId("100000000000000000000000") ]
}
}
}
},
date: 1,
messageContent: 1,
to: 1,
from: 1
}
},
{
$sort: { "date": -1 }
},
{
$group: {
_id: "$whom",
date: { "$first": "$date" },
message: { "$first": "$messageContent" },
to: { "$first": "$to" },
from: { "$first": "$from" }
}
}
])
Result:
[
{
"_id": ObjectId("300000000000000000000000"),
"date": ISODate("2021-09-04T11:23:25.184Z"),
"message": "333"
},
{
"_id": ObjectId("200000000000000000000000"),
"date": ISODate("2021-09-03T11:23:25.184Z"),
"message": "222"
}
]
Example: mongoplayground