0

I am using lambda function, python 3.6 and Mongodb atlas. In mongodb i have one collection below like this. collection name profile. below as the collection structure.

"_id" : ObjectId("5db234df92b0ce00016932f3")
"username" : "testing"
"channel" : [ "abc", "efg", "cde", "xyz" ]
"about" : "this is a test case"

we have multiple rows similar to the above. Now i am using python, i write the lambda function to find strings matched letter in channel array.find the below lambda function.

profile = db.profile
name = event['cname']

ch = list(profile.aggregate([{
    "$match" : { "username" : "testing" }
    }, 
    {
        "$project" : {
            "channel" : 1
            }
    }
    ]))

ch1 = json.loads(json.dumps(ch, default=json_util.default))
ch2 = [document["channel"] for document in ch1]
new_list = []
for i in ch2:
    if(re.findall(name, i)):
        new_list.append(i)  
return new_list

I have passed "cname" : "c" in event. but i am getting error like this.

Response:
{
 "errorMessage": "expected string or bytes-like object",
 "errorType": "TypeError",
 "stackTrace": [
[
  "/var/task/lambda_function.py",
  51,
  "lambda_handler",
  "if(re.findall(search, i)):"
],
[
  "/var/lang/lib/python3.6/re.py",
  222,
  "findall",
  "return _compile(pattern, flags).findall(string)"
]
]
}

I tried with re.search also but i am getting same, I need output like this below.

Input: "cname" : "c"
output: "abc"
        "cde"

can you please help me with solution, thanks on advance.

7
  • Can you print and check what you're getting in name & ch, check the type of those two, as in your other question name should be a string & ch should be an array/list of strings !! Something like this :: type(name), if they're not string then you've to convert those like str(name) Commented Nov 1, 2019 at 14:28
  • @srinivasy I have checked in "name" what i have passed in "event", it is display in "name" and in "ch", array list is display Commented Nov 1, 2019 at 14:36
  • I mean to say you need to check all values inside ch2 should be strings & also name should be a string, is that what you're seeing ? Commented Nov 1, 2019 at 14:52
  • @srinivasy bro I have checked values, all are in strings. when i print ch2: [['abc', 'efg', 'cde', 'xyz' ]]. I removed one bracket using ch3 = (', '.join(map(str, ch2))). now i am getting array only single brackets. now it's working but i getting the output like this, when i pass "c" as a input, output like [ "c", "c" ] not display the matched entire strings Commented Nov 1, 2019 at 19:51
  • Can you put the entire code here, is should not happen like that when name is a string which is checked against list of strings.. Commented Nov 1, 2019 at 20:02

1 Answer 1

0

Follow the below code:

 ch2 = [document["channel"] for document in ch]
 new_list = []

 for word in ch2[0]:
    print(word)
    if(re.findall(name, word)):
        new_list.append(word)

  return new_list

the issue has been resolved

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

Comments

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.