我有两个集合 1) user_posts 2)user_profile。找到以下采集数据供您参考。
1)user_posts集合
_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集合
_id : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh",
photo : " ",
created : " ",
modified : " "
_id : ObjectId("5d518da6d55bc00001d235c2")
username : "shekar",
photo : " ",
created : " ",
modified : " "
现在我尝试从 lambda 函数中的 user_profile 获取个人资料详细信息。但我没有得到详细信息。找到下面的 lambda 函数代码。
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)
但我没有得到输出。我需要像下面这样的输出。
_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 : " "
你能帮我解决方案吗?提前致谢。