0

I am using python in Lamnda function to execute this code. I have passed two values in event user_id and status.

def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=1))
community_users = db.community_users

user_id = event['user_id']
status = event['status']

ch2 = [document for document in user_id]
for u_id in ch2:
community_users.insert({ "user_id" : ObjectId(u_id), "status" : 
ObjectId(status)})
return 0

Here I have passed status values in array like this.

 [5dc4a8b7360a0100012d3ec8, 5dc1f2d14a59120001a4d012, 
  5dc1f2d14a59120001a4d12c, 5dc1f2d14a59120001a4d12c]

now I need to store status values as individual documents. I tried 'for' operator but it was stored only array first value. I need output like this below.

{ userid: ObjectId("5dc1f2ed4a59120001a4d09d"), status: 
  ObjectId("5dc4a8b7360a0100012d3ec8"}),
{ userid: ObjectId("5dc1f2ed4a59120001a4d09d"), status: 
  ObjectId("5dc1f2d14a59120001a4d012"}),
{ userid: ObjectId("5dc1f2ed4a59120001a4d09d"), status: 
  ObjectId("5dc1f2d14a59120001a4d12c"}),
{ userid: ObjectId("5dc1f2ed4a59120001a4d09d"), status: 
  ObjectId("5dc1f2d14a59120001a4d12c"})

Can you please help me out with the answer. thanks in advance

1 Answer 1

1

There's a wrong indentation there. You need to indent the line after the FOR loop:

for u_id in ch2:
   community_users.insert({ "user_id" : user_id, "status" : u_id })

See Python indentation

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

2 Comments

I have done the same but array first value only inserted into mongodb.
it's working. i have write return 0 outside for loop, it's working.

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.