简体   繁体   English

How to search matched list in Json output using python in lambda function?

[英]How to search matched list in Json output using python in lambda function?

I have two collections community and profile.我有两个 collections 社区和个人资料。

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.我像下面这样写 lambda function。

 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:我已经执行了 lambda function,我得到了 output,如下所示:

[
  {
    "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.提前致谢。

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.请根据您的要求动态传递/eesh/i 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.另一方面,我尝试在$lookup本身中添加此过滤器{ "details.username": /eesh/i } - 这样我们可以避免遵循$unwind$match阶段,但是因为我们有两个过滤器进行检查 - 由于在$match$expr中使用regex的限制很少,所以这个查询是唯一的,甚至这更容易做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM