使用 $regex 过滤数组元素 [英] Filter array elements with $regex

查看:79
本文介绍了使用 $regex 过滤数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ///sample Data 
{
    "_id" : "CUST1234",
    "Phone Number" : "9585290750",
    "First Name" : "jeff",
    "Last Name" : "ayan",
    "Email ID" : "",
    "createddate" : 1462559400000.0,
    "services" : [ 
        {
            "type" : "Enquiry",
            "timeSpent" : "0:00",
            "trxID" : "TRXE20160881",
            "CustomerQuery" : "Enquiry about travell agent numbers in basaveshwara nagara",
            "ServiceProvided" : "provided info through whatsapp",
            "Category" : "Tours/Travels",
            "callTime" : "2016-05-06T18:30:00.000Z",
            "ActualAmount" : 0,
            "FinalAmount" : 0,
            "DiscountRuppes" : 0,
            "DiscountPerctange" : 0
        }, 
        {
            "type" : "Enquiry",
            "timeSpent" : "0:00",
            "trxID" : "TRXE20160882",
            "CustomerQuery" : "Enquiry about Electric bill payment of house",
            "ServiceProvided" : "Service provided",
            "Category" : "Utility Services",
            "callTime" : "2016-05-10T18:30:00.000Z",
            "ActualAmount" : 0,
            "FinalAmount" : 0,
            "DiscountRuppes" : 0,
            "DiscountPerctange" : 0
        }, 
        {
            "type" : "Enquiry",
            "timeSpent" : "0:00",
            "trxID" : "TRXE20160883",
            "CustomerQuery" : "Enquiry about KPSC office number",
            "ServiceProvided" : "provided info through whatsapp",
            "Category" : "Govt Offices/Enquiries",
            "callTime" : "2016-05-13T18:30:00.000Z",
            "ActualAmount" : 0,
            "FinalAmount" : 0,
            "DiscountRuppes" : 0,
            "DiscountPerctange" : 0
        }, 
        {
            "type" : "Enquiry",
            "timeSpent" : "0:00",
            "trxID" : "TRXE20160884",
            "CustomerQuery" : "Enquiry about Sagara appolo hospital contact number",
            "ServiceProvided" : "provided the information through call",
            "Category" : "Hospitals/Equipments",
            "callTime" : "2016-05-14T18:30:00.000Z",
            "ActualAmount" : 0,
            "FinalAmount" : 0,
            "DiscountRuppes" : 0,
            "DiscountPerctange" : 0
        },
 ]
}

预期输出:与服务"字段中搜索框中特定字符串匹配的整个数据.

Expected Output : entire data that matches particular string in search box from "services" field.

     db.collection.aggregate([
        { 
            $match: { 
                "Phone Number": "9585290750", 
                "services": { $regex: "/^t/", $options: "s i" }
            }
        },                     
        {
            $project: {
                "Services": "services" 
            }
        }
    ]);

我在上述集合中的正则表达式部分遇到问题,services 是一个数组字段.请帮我过滤数据.

I am facing an issue in regex portion in the above Collection, services is an array field. Please help me to filter the data.

推荐答案

伙计们,因为我是 Mongodb 的新手,所以我花了一天时间才找到适合我的任务的解决方案.我有我的问题的解决方案.如果你们有比这更好的查询,只需发布​​或修改它....

Guys since i am new to Mongodb it took me a day to find a proper solution to my task. I have a solution to my issue. If you guys have better query than this, just post it or modify it....

 db.collections.aggregate([
        {"$match":{"Corporate_ID":"id"}},
        {"$unwind":"$services"},
        {"$match":{"$or":[
            {"services.type":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.timeSpent":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.trxID":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.CustomerQuery":{$regex:'F',"$options": "i"}},
            {"services.ServiceProvided":{$regex:'F',"$options": "i"}},
            {"services.Category":{$regex:'F',"$options": "i"}},
            {"services.callTime":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.ActualAmount":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.FinalAmount":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.DiscountRuppes":{$regex:'TRXF2016088142',"$options": "i"}},
            {"services.DiscountPerctange":{$regex:'TRXF2016088142',"$options": "i"}}                     
            ]}},
        {"$unwind":"$services"},
        {"$project":{
            "service":"$services"}
               }        
])

这篇关于使用 $regex 过滤数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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