Podes tentar,
$addFields
para criar um array exclusivo chamadouserIds
forma ambas as matrizesfollowers
efollowings
,$setUnion
para obter IDs exclusivos,$lookup
com coleção de usuários$project
para mostrar campos,followers
obter fullName, $map para iterar o loop defollowers
e obtenha o nome defollowerId
da matriz de usuários usando$reduce
e$cond
followers
obtenha fullName, $map para iterar o loop defollowings
e obtenha o nome defollowingId
da matriz de usuários usando$reduce
e$cond
db.followings.aggregate([
{
$addFields: {
userIds: {
$setUnion: [
{
$map: {
input: "$followers",
in: "$$this.followerId"
}
},
{
$map: {
input: "$followings",
in: "$$this.followingId"
}
}
]
}
}
},
{
$lookup: {
from: "users",
localField: "userIds",
foreignField: "_id",
as: "users"
}
},
{
$project: {
userId: 1,
followers: {
$map: {
input: "$followers",
as: "f",
in: {
$mergeObjects: [
"$$f",
{
fullName: {
$reduce: {
input: "$users",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$f.followerId"] },
"$$this.fullName",
"$$value"
]
}
}
}
}
]
}
}
},
followings: {
$map: {
input: "$followings",
as: "f",
in: {
$mergeObjects: [
"$$f",
{
fullName: {
$reduce: {
input: "$users",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$f.followingId"] },
"$$this.fullName",
"$$value"
]
}
}
}
}
]
}
}
}
}
}
])
Playground