如何使用lambda函数在mongodb中的两个集合之间进行多个联接? [英] How to do multiple joins between two collection in mongodb using lambda function?
问题描述
我有两个集合1)user_posts 2)user_profile.找到以下收集数据以供参考.
I have two collections 1) user_posts 2)user_profile. find the below collection data for your reference.
1)user_posts集合
1) user_posts collection
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welcome"
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
2)User_profile集合
2) User_profile collection
_id : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh",
photo : " ",
created : " ",
modified : " "
_id : ObjectId("5d518da6d55bc00001d235c2")
username : "shekar",
photo : " ",
created : " ",
modified : " "
现在,我尝试从lambda函数中的user_profile获取配置文件详细信息.但我没有得到细节.找到下面的lambda函数代码.
Now i tried to get the profile details from user_profile in lambda function. but i didn't get the details. find the below lambda function code.
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=1))
user_posts = db.user_posts
Userid = event['userid']
uid = ObjectId(Userid)
dispost = list(user_posts.aggregate([{
"$match" : { "userid" : uid }
},
{ "$graphLookup" :
{
"from" : "user_profile",
"startWith" : "$like.userid",
"connectFromField" : "like.userid",
"connectToField" : "_id",
"as" : "userdetails"
}
},
{ "$graphLookup" :
{
"from" : "user_profile",
"startWith" : "$comment.userid",
"connectFromField" : "comment.userid",
"connectToField" : "_id",
"as" : "userdetails1"
}
}
{ "$graphLookup" :
{
"from" : "user_profile",
"startWith" : "$share.userid",
"connectFromField" : "share.userid",
"connectToField" : "_id",
"as" : "userdetails2"
}
}
]))
disair = json.dumps(dispost, default=json_util.default)
return json.loads(disair)
但是我没有得到输出.我需要下面这样的输出.
but i didn't get the output. i need output like this below.
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh"
photo : " ",
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
username : "ramesh"
photo : " "
1 : Object
username : "shekar"
photo : " "
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
username : "shekar"
photo : " "
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welocme"
username : "shekar"
photo : " "
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
username : "shekar"
photo : " "
能帮我解决问题吗?预先感谢.
can you please help me the solutions. Thanks in advance.
推荐答案
请检查以下内容:
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
这篇关于如何使用lambda函数在mongodb中的两个集合之间进行多个联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!