0

I have two collection 1) ram 2) details. find the below pic for your reference.RAM collection details

details collection details

In the lambda function, when i passed the rank in event, we will get the output the related rank. but i need more details in the same output.

Eg: when i passed the rank event 10002, then we will get the rank 10002 relevant data display. but i need along with user details like ramesh, suresh
details from details collection.

def lambda_handler(event, context):

print("Received event: " + json.dumps(event, indent=1))

posts = db.user_posts

rankid = event['rank']

disposts = list(posts.find({"rank" : rankid}))
posts = json.dumps(disposts, default=json_util.default)
return json.loads(posts)

In this above code, i will get only one collection data. I need both collections data on same lambda function.

My expected output like this below.

rank : 10002,
details: 
      0 : Object
        username : "ramesh",
        des : "developer",
        edu : "b.tech",
        age : 25
      1 : Object
        username : "suresh",
        des : "admin",
        edu : "mba",
        age : 27

not exactly, i need the output similar.

Can you please help me out with the proper code. Thank you

0

1 Answer 1

1

Try this query :

 db.getCollection('ram').aggregate([{ $match: { rank: '10002' } }, { $unwind: '$details' }, {
    $lookup:
    {
        from: "details",
        let: { userName: '$details.username' },
        pipeline: [
            {
                $match:
                {
                    $expr:
                    {
                        $eq: ['$$userName', '$username']
                    }
                }
            }
        ],
        as: "user_details"
    }
}, { $addFields: { 'user_details.des': '$details.des' } }, { $group: { _id: '$rank', details: { "$push": { $arrayElemAt: ["$user_details", 0] } } } }, { $project: { rank: '$_id', _id: 0, details: 1 } }])

Maybe you need to consider storing des field of RAM collection in details collection, that way you'll only have username mapped to username field in details collection, it would be easy that way.

Sign up to request clarification or add additional context in comments.

6 Comments

HI bro, thanks for your valuable information. I tried some other joins based on the above solutions but i get an empty out. i have explain clearly with expected output. please find the below stackoverflow.com/questions/57397863/… Can you please help out with the changes bro. thanks you
@RameshReddy : Can you give me collection data as text, giving me pics is like I need to input those things in DB each one, that's a long job..!!
bro if you have time, can you please give me the solution for this query stackoverflow.com/questions/57808466/…
@RameshReddy : that should be possible within code(saying with general programming knowledge - but not sure how difficult it is with python), but I'm very beginner in python, So I can't help on that !
bro if you have time, can you please help with answer for the below query stackoverflow.com/questions/57996739/…
|

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.