0

I have two collection 1) profile ,2) posts find the below pic for your reference. user_prfoile collection

user_posts collection

In the lambda function, when i passed the userid, then we will get the userid revlevant data display. but i need user details in feelings array.

I tried with the below code, but i get the empty output

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)

I get an empty output.

I need output like this below.

_id :
userid : 
type : 
location :
feelings : 
   [ {userid : objectid(""),
     username : "nipun",
     photo : " "},
     {userid : objectid(""),
     username : "ramesh",
     photo : " "}
   ] 

in feelings array , i need user details from user_profile collection based on userid in feelings array.

1
  • you've to do $unwind on feelings array of user_posts. Please provide documents in text format rather than images. Commented Aug 7, 2019 at 15:50

1 Answer 1

1

Ok, there are couple of issues with that aggregation query, keeping most of it as is, I've modified it to work, as of now please try this and make any changes needed :

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 } }
])
Sign up to request clarification or add additional context in comments.

14 Comments

Thank you so much bro. it's working fine. I have one small query bro, In feeling array i need username and photo only from user_profile collection. How can i remove other columns.
@RameshReddy : Quick thing is to do "feelings.username": 1, "feelings.photo": 1 in $project, or if you really think there are a lot of fields to be added then use $addFields, while on mapping if possible only map to necessary fields !!
bro above solutions working , i have one more last query. In user_post, i have userid right. i need username, photo based on userid. I tried one more join but it is not working.
@RameshReddy : that’s what this query does, do you see any issue in it ?
no bro, my query is we have two userid in document of user_posts collection. previously we are working on feelings userid. now i need outside userid user details along with feelings userid user details.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.