如何使用mongodb组合lambda函数中的两个输出?
我有两个收集1)个人资料,2)帖子找到下面的图片供你参考 在lambda函数中,当我传递userid时,我们将获得userid revelvant数据显示。但是我需要用户的详细信息 我尝试了下面的代码,但得到了空输出如何使用mongodb组合lambda函数中的两个输出?,mongodb,aws-lambda,pymongo,Mongodb,Aws Lambda,Pymongo,我有两个收集1)个人资料,2)帖子找到下面的图片供你参考 在lambda函数中,当我传递userid时,我们将获得userid revelvant数据显示。但是我需要用户的详细信息 我尝试了下面的代码,但得到了空输出 def lambda_handler(event, context): print("Received event: " + json.dumps(event, indent=1)) Userid = event['userid'] uid = ObjectId(Useri
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=1))
Userid = event['userid']
uid = ObjectId(Userid)
dispost = list( db.user_posts.aggregate([{ "$match" : { "userid" : uid } },
{ "$unwind" : "$user_profile" },
{"$lookup":
{
"from" : 'user_profile',
"localField" : 'feelings.userid',
"foreignField" : '_id',
"as" : 'user_details'
}
},
{ "$addFields" : { "user_details.feelings.username" : "$user_profile.username", "user_details.feelings.photo" : "$user_profile.photo"}},
{ "$group" : {
"_id" : "$_id",
"user_profile" : {
"$push" : {
"$arrayElemAt" : ["$user_details", 0]
}}}},
{ "$project" : { "_id" : "$_id", "userid" : 1, "type" : 1, "feelings" : 1 }}
]))
disair = json.dumps(dispost, default=json_util.default)
return json.loads(disair)
我得到一个空输出
我需要像下面这样的输出
_id :
userid :
type :
location :
feelings :
[ {userid : objectid(""),
username : "nipun",
photo : " "},
{userid : objectid(""),
username : "ramesh",
photo : " "}
]
在Feelations数组中,我需要根据Feelations数组中的用户ID从user_profile collection中获取用户详细信息。好的,聚合查询存在两个问题,保持大部分不变,我已将其修改为可用,到目前为止,请尝试此操作并进行所需更改:
db.getCollection('user_posts').aggregate([{ "$match": { "userid": uid } },
{ "$unwind": "$feelings" },
{
"$lookup":
{
"from": 'user_profile',
"localField": 'feelings.userid',
"foreignField": '_id',
"as": 'feelings'
}
},
{
"$group": {
"_id": "$_id", userPost: { "$first": "$$CURRENT" }, "feelings": {
"$push": { "$arrayElemAt": ["$feelings", 0] }
}
}
}, { "$project": { userid: '$userPost.userid', type: '$userPost.type', "feelings": 1 } }
])
你必须在用户帖子的情感数组上做$REWIND。请提供文本格式的文件而不是图片。非常感谢兄弟。很好用。我有一个小查询兄弟,在感觉数组中,我只需要用户名和照片从用户_个人资料收集。如何删除其他列。@Rameshredy:最快的方法是在$project中执行“feelations.username”:1,“feelations.photo”:1,或者如果您真的认为有很多字段需要添加,则使用$addFields,而在映射时,如果可能,只映射到必要的字段!!兄弟,以上解决方案有效,我还有最后一个问题。在user_post中,我拥有userid权限。我需要用户名,基于用户名的照片。我又尝试了一个连接,但它不起作用。@Rameshredy:这就是这个查询的作用,你看到其中有什么问题吗?没有,兄弟,我的查询是我们在user_posts集合的文档中有两个userid。之前我们正在研究用户ID。现在我需要外部用户ID用户详细信息以及用户ID用户详细信息。