简体   繁体   English

用$ regex过滤数组元素

[英]Filter array elements with $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" 
            }
        }
    ]);

I am facing an issue in regex portion in the above Collection, services is an array field. 我在上述集合的正则表达式部分遇到问题, services是一个数组字段。 Please help me to filter the data. 请帮助我过滤数据。

This is because you are passing in a string of JavaScript regular expression object to $regex . 这是因为您要将JavaScript正则表达式对象的字符串传递给$regex Change your regex to one of the following. 将您的正则表达式更改为以下之一。

"service": { "$regex": /^t/, "$options": "si" }

or 要么

"service": { "$regex": "^t", "$options": "si" }

Guys since i am new to Mongodb it took me a day to find a proper solution to my task. 伙计们,自从我刚接触Mongodb以来,我花了一天的时间才能找到适合自己任务的解决方案。 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"}
               }        
])

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

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