如何在Lambda函数中使用python搜索Json输出中的匹配列表? [英] How to search matched list in Json output using python in lambda function?

查看:123
本文介绍了如何在Lambda函数中使用python搜索Json输出中的匹配列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个收藏集社区和个人资料.

I have two collections community and profile.

社区收集样本数据:

_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"

我在下面编写这样的lambda函数.

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

我已经执行了lambda函数,我得到的输出如下:

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.

推荐答案

需要细微调整才能完成工作,以下是查询:

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"
    }
}]))

请根据您的要求动态传递/eesh/i.换句话说,我尝试在$lookup本身中添加此过滤器{ "details.username": /eesh/i }-这样我们就可以避免遵循$unwind& $match阶段,但是由于要检查两个过滤器-由于$match$expr中使用regex的限制很少,因此此查询为&甚至更容易做到.

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.

这篇关于如何在Lambda函数中使用python搜索Json输出中的匹配列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆