कृपया इसे जांचें:
db.collection("user_posts").aggregate(
{ $match: {"userid" : uid}},
{ $unwind: '$like' },
{ $lookup: { from: "users", localField: "like.userid", foreignField: "_id", as:
"users" }},
{ $group: {
_id: "$_id",
like: { $push: { $mergeObjects: ['$like', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { like: "$like"} ]} } },
{ $unwind: '$comment' },
{ $lookup: { from: "users", localField: "comment.userid", foreignField: "_id", as:
"users" }},
{ $group: {
_id: "$_id",
comment: { $push: { $mergeObjects: ['$comment', { $arrayElemAt: [ "$users", 0
] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { comment: "$comment"} ]} } },
{ $unwind: '$share' },
{ $lookup: { from: "users", localField: "share.userid", foreignField: "_id", as:
"users" }},
{ $group: {
_id: "$_id",
share: { $push: { $mergeObjects: ['$share', { $arrayElemAt: [ "$users", 0 ] }
]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { share: "$share"} ]} } },
{ $project: { users: 0 }}
)
आपको आउटपुट मिलेगा, अपनी आवश्यकता के अनुसार प्रोजेक्ट एग्रीगेशन में फ़ील्ड नाम जोड़ें/हटाएं बदलें