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

前端 未结 2 738
轮回少年
轮回少年 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 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 !!

提交回复
热议问题