How to do multiple joins between two collection in mongodb using lambda function?

前端 未结 2 739
轮回少年
轮回少年 2021-01-16 20:12

I have two collections 1) user_posts 2)user_profile. find the below collection data for your reference.

1) user_posts collection

_id :ObjectId(\"5d51         


        
相关标签:
2条回答
  • 2021-01-16 20:57

    Please check this:

    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 }}
    )
    

    you will get output, changes add/remove fields name in project aggregation as per your requirement

    0 讨论(0)
  • 2021-01-16 21:07

    Please check this :

        db.user_posts.aggregate([{ $match: { _id: ObjectId("5d519f861c9d4400005ebd1b") } }, {
        $lookup:
        {
            from: "user_profile",
            let: { userIdToBeCompared: "$userid", like: '$like', comment: '$comment', share: '$share' },
            pipeline: [
                {
                    $match:
                    {
                        $expr:
                        {
    
                            $or:
                                [
                                    { $eq: ["$_id", "$$userIdToBeCompared"] },
                                    { $in: ["$_id", "$$like.userid"] },
                                    { $in: ["$_id", "$$comment.userid"] },
                                    { $in: ["$_id", "$$share.userid"] }
                                ]
                        }
                    }
                }
    
            ],
            as: "data"
        }
    }])
    

    Ok there are way too many things has to happen in your requirement and with your current data structure it might not be an easy thing & a good idea to implement all your needs from your DB layer, So over here final response data will have all the mappings for your needs, get it to your code and fetch the needed details to each section based on userid, as of what I've tried I thought that would be ideal scenario to implement !!

    0 讨论(0)
提交回复
热议问题