$map за повторение на цикъла на subRoom масив и in за връщане на задължителни полета
$filter за повторение на цикъл от notifications и $size за получаване на общия брой елементи от филтрирания резултат
db.collection.find({ id: 1 },
{
_id: 0,
room: 1,
notRead: {
$size: {
$filter: {
input: "$notifications",
cond: {
$not: { $in: ["User1", "$$this.read"] }
}
}
}
},
"subRoom": {
$map: {
input: "$subRoom",
in: {
id: "$$this.id",
notRead: {
$size: {
$filter: {
input: "$$this.notifications",
cond: { $not: { $in: ["User1", "$$this.read"] } }
}
}
}
}
}
}
})
Playground