1

I have two collections community and profile.

community collection sample data:

_id : ObjectId("5dc1f30abcafe70001bd0075")
communityname : "testing1",
user_id : ObjectId("5dc1f2ed4a59120001a4d09d")
active_flag : 0

_id : ObjectId("5dc1f30abcafe70001bd0082")
communityname : "testing1",
user_id : ObjectId("5dc4a8b7360a0100012d3ec8")
active_flag : 0

profile collection sample data:

_id : ObjectId("5dc1f2ed4a59120001a4d09d"),
username : "Haneesh",
"name" : "hani"

_id : ObjectId("5dc4a8b7360a0100012d3ec8"),
username : "Harish",
name : "Hari"

I write the lambda function like this below.

 community =db.community

    comm_id = ObjectId(event['c_id'])

    user1 = list(community.aggregate([{
        "$match" : { "_id" : comm_id }
        }, 
        {
            "$lookup" : {
                "from" : "profile",
                "localField" : "user_id",
                "foreignField" : "_id",
                "as" : "details"
            }
        },
        { "$unwind" : "$details" },
        {
            "$project" : {
                "_id" : 0,
                "username" : "$details.username",
                "name" : "$details.name"
                }
        }
        ]))

    user2 = json.loads(json.dumps(user1, default=json_util.default))
    return user2

I have executed the lambda function, I'm getting output like this below:

[
  {
    "username": "anvithpm026",
    "name": "Anvith P M"
  },
  {
    "username": "shailu",
    "name": "shail"
  },
  {
    "username": "sukumar",
    "name": "suku"
  }
]

Now my concern is, how to search matched list. Example if a letter matchs with the username, then it get the username along with name. I've tried to use the regular expression but it didn't work. Please help me with a solution. Thanks in advance.

7
  • @srinivasy I have edited my question. Commented Nov 13, 2019 at 19:32
  • Hey quick question, seems like I miss understood your question, After looking into your collections I understood you've to keep that query as is, So do you want to make this work on python code ? On output array of aggregation ? Where do you or how do you pass username ? Commented Nov 13, 2019 at 19:32
  • Also your question is so confusing if you're doing $match to filter documents ("$match" : { "_id" : comm_id }) in first step on unique ID(_id), then all you'll get is an object in response !! how are you getting an array in output ?? Commented Nov 13, 2019 at 19:44
  • @srinivasy every community has mutliple userid and we have multiple communities. so I write the match condition first and getting only one particular community user_id then lookup with profile collection to get the username and name Commented Nov 13, 2019 at 19:52
  • 1
    @srinivasy yes bro Commented Nov 13, 2019 at 20:01

1 Answer 1

1

A minor tweak does the job, below is the query :

user1 = list(community.aggregate([{
    "$match": { "_id": ObjectId("5dc1f30abcafe70001bd0075") }
},
{
    "$lookup": {
        "from": "profile",
        "localField": "user_id",
        "foreignField": "_id",
        "as": "details"
    }
}, { "$unwind": "$details" }, { "$match": { "details.username": /eesh/i } }, {
    "$project": {
        "_id": 0,
        "username": "$details.username",
        "name": "$details.name"
    }
}]))

Please pass /eesh/i dynamically as per your requirement. In the other way I've tried to add this filter { "details.username": /eesh/i } in $lookup itself - that way we can avoid following $unwind & $match stages, But as we've two filters to be checked - due to few limitations on using regex in $expr of $match, this query is the one & even this is easier to do.

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

1 Comment

Bro, if you don't mine can you please give the solution of the below query stackoverflow.com/questions/58937282/…

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.