आप कोशिश कर सकते हैं,
$addFields
userIds
called नामक एक अद्वितीय सरणी बनाने के लिए दोनों सरणियाँ बनाएंfollowers
औरfollowings
,$setUnion
अद्वितीय आईडी प्राप्त करने के लिए,$lookup
उपयोगकर्ता संग्रह के साथ$project
फ़ील्ड दिखाने के लिए,followers
followers
. के लूप को पुनरावृत्त करने के लिए fullName, $map प्राप्त करें औरfollowerId
. का नाम प्राप्त करें$reduce
. का उपयोग करके उपयोगकर्ता सरणी से और$cond
followings
followings
. के लूप को पुनरावृत्त करने के लिए fullName, $map प्राप्त करें औरfollowingId
. का नाम प्राप्त करें$reduce
. का उपयोग करके उपयोगकर्ता सरणी से और$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"
]
}
}
}
}
]
}
}
}
}
}
])